From e90315ab5543184913f470217bf6342fc3b407ea Mon Sep 17 00:00:00 2001 From: Marcin Rogoz Date: Thu, 24 Mar 2022 10:12:57 +0100 Subject: [PATCH 01/12] Added MongoObjectIdSerializer for String and NullableString --- .../StronglyTypedIdConverter.cs | 5 +++++ ...verterAndBackingTypeCombinationDiagnostic.cs | 17 +++++++++++++++++ src/StronglyTypedIds/EmbeddedSources.cs | 10 ++++++++++ src/StronglyTypedIds/Parser.cs | 4 ++++ src/StronglyTypedIds/SourceGenerationHelper.cs | 11 +++++++++++ .../StronglyTypedIdConverterExtensions.cs | 10 ++++++++++ .../NullableString_MongoObjectIdSerializer.cs | 15 +++++++++++++++ .../String/String_MongoObjectIdSerializer.cs | 15 +++++++++++++++ .../StronglyTypedIds.IntegrationTests.csproj | 1 + .../Types/StringId.cs | 3 +++ 10 files changed, 91 insertions(+) create mode 100644 src/StronglyTypedIds/Diagnostics/InvalidConverterAndBackingTypeCombinationDiagnostic.cs create mode 100644 src/StronglyTypedIds/Templates/NullableString/NullableString_MongoObjectIdSerializer.cs create mode 100644 src/StronglyTypedIds/Templates/String/String_MongoObjectIdSerializer.cs diff --git a/src/StronglyTypedIds.Attributes/StronglyTypedIdConverter.cs b/src/StronglyTypedIds.Attributes/StronglyTypedIdConverter.cs index 599a51409..3df4377fb 100644 --- a/src/StronglyTypedIds.Attributes/StronglyTypedIdConverter.cs +++ b/src/StronglyTypedIds.Attributes/StronglyTypedIdConverter.cs @@ -46,5 +46,10 @@ public enum StronglyTypedIdConverter /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Mongo Serializer for converting string to and from ObjectId + /// + MongoObjectIdSerializer = 64, } } \ No newline at end of file diff --git a/src/StronglyTypedIds/Diagnostics/InvalidConverterAndBackingTypeCombinationDiagnostic.cs b/src/StronglyTypedIds/Diagnostics/InvalidConverterAndBackingTypeCombinationDiagnostic.cs new file mode 100644 index 000000000..88dee4544 --- /dev/null +++ b/src/StronglyTypedIds/Diagnostics/InvalidConverterAndBackingTypeCombinationDiagnostic.cs @@ -0,0 +1,17 @@ +using Microsoft.CodeAnalysis; + +namespace StronglyTypedIds.Diagnostics +{ + internal static class InvalidConverterAndBackingTypeCombinationDiagnostic + { + internal const string Id = "STI6"; + internal const string Message = "The StronglyTypedIdBackingType and StronglyTypedIdConverter values provided are not a valid combination of flags"; + internal const string Title = "Invalid converter and backing type combination"; + + public static Diagnostic Create(SyntaxNode currentNode) => + Diagnostic.Create( + new DiagnosticDescriptor( + Id, Title, Message, category: Constants.Usage, defaultSeverity: DiagnosticSeverity.Error, isEnabledByDefault: true), + currentNode.GetLocation()); + } +} diff --git a/src/StronglyTypedIds/EmbeddedSources.cs b/src/StronglyTypedIds/EmbeddedSources.cs index e1f229779..0bd6b6512 100644 --- a/src/StronglyTypedIds/EmbeddedSources.cs +++ b/src/StronglyTypedIds/EmbeddedSources.cs @@ -25,6 +25,7 @@ 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"), + string.Empty, false ); @@ -37,6 +38,7 @@ 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"), + string.Empty, false ); @@ -49,6 +51,7 @@ 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"), + string.Empty, false ); @@ -61,6 +64,7 @@ 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_MongoObjectIdSerializer.cs"), false ); @@ -73,6 +77,7 @@ 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_MongoObjectIdSerializer.cs"), true ); @@ -85,12 +90,14 @@ 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"), + string.Empty, false ); internal const string TypeConverterAttributeSource = " [System.ComponentModel.TypeConverter(typeof(TESTIDTypeConverter))]"; internal const string NewtonsoftJsonAttributeSource = " [Newtonsoft.Json.JsonConverter(typeof(TESTIDNewtonsoftJsonConverter))]"; internal const string SystemTextJsonAttributeSource = " [System.Text.Json.Serialization.JsonConverter(typeof(TESTIDSystemTextJsonConverter))]"; + internal const string MongoObjectIdSerializerAttributeSource = " [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(TESTIDMongoObjectIdSerializer))]"; internal static string LoadEmbeddedResource(string resourceName) { @@ -116,6 +123,7 @@ public readonly struct ResourceCollection public string TypeConverter { get; } public string EfCoreValueConverter { get; } public string DapperTypeHandler { get; } + public string MongoObjectId { get; } public string Comparable { get; } public ResourceCollection( @@ -127,6 +135,7 @@ public ResourceCollection( string efCoreValueConverter, string dapperTypeHandler, string comparable, + string mongoObjectId, bool nullableEnable) { BaseId = baseId; @@ -137,6 +146,7 @@ public ResourceCollection( DapperTypeHandler = dapperTypeHandler; Comparable = comparable; NullableEnable = nullableEnable; + MongoObjectId = mongoObjectId; Header = header; } } diff --git a/src/StronglyTypedIds/Parser.cs b/src/StronglyTypedIds/Parser.cs index 39fc96f93..d3b379cba 100644 --- a/src/StronglyTypedIds/Parser.cs +++ b/src/StronglyTypedIds/Parser.cs @@ -195,6 +195,10 @@ public static bool IsAttributeTargetForGeneration(SyntaxNode node) { reportDiagnostic(InvalidBackingTypeDiagnostic.Create(structDeclarationSyntax)); } + else if (!converter.IsValidCombination(backingType)) + { + reportDiagnostic(InvalidConverterAndBackingTypeCombinationDiagnostic.Create(structDeclarationSyntax)); + } if (!implementations.IsValidFlags()) { diff --git a/src/StronglyTypedIds/SourceGenerationHelper.cs b/src/StronglyTypedIds/SourceGenerationHelper.cs index d08ffc1e9..da6e6963f 100644 --- a/src/StronglyTypedIds/SourceGenerationHelper.cs +++ b/src/StronglyTypedIds/SourceGenerationHelper.cs @@ -69,6 +69,7 @@ static string CreateId( var useSystemTextJson = converters.IsSet(StronglyTypedIdConverter.SystemTextJson); var useEfCoreValueConverter = converters.IsSet(StronglyTypedIdConverter.EfCoreValueConverter); var useDapperTypeHandler = converters.IsSet(StronglyTypedIdConverter.DapperTypeHandler); + var useMongoObjectIdSerializer = converters.IsSet(StronglyTypedIdConverter.MongoObjectIdSerializer); var useIEquatable = implementations.IsSet(StronglyTypedIdImplementations.IEquatable); var useIComparable = implementations.IsSet(StronglyTypedIdImplementations.IComparable); @@ -121,6 +122,11 @@ static string CreateId( { sb.AppendLine(EmbeddedSources.TypeConverterAttributeSource); } + + if (useMongoObjectIdSerializer) + { + sb.AppendLine(EmbeddedSources.MongoObjectIdSerializerAttributeSource); + } sb.Append(resources.BaseId); ReplaceInterfaces(sb, useIEquatable, useIComparable); @@ -156,6 +162,11 @@ static string CreateId( { sb.AppendLine(resources.SystemTextJson); } + + if (useMongoObjectIdSerializer) + { + sb.AppendLine(resources.MongoObjectId); + } sb.Replace("TESTID", idName); sb.AppendLine(@" }"); diff --git a/src/StronglyTypedIds/StronglyTypedIdConverterExtensions.cs b/src/StronglyTypedIds/StronglyTypedIdConverterExtensions.cs index 63d4174b2..7a748c604 100644 --- a/src/StronglyTypedIds/StronglyTypedIdConverterExtensions.cs +++ b/src/StronglyTypedIds/StronglyTypedIdConverterExtensions.cs @@ -23,5 +23,15 @@ public static bool IsValidFlags(this StronglyTypedIdImplementations value) { return (int)value >= 0 && (int)value < _maxImplementationsId; } + + public static bool IsValidCombination(this StronglyTypedIdConverter value, StronglyTypedIdBackingType backingType) + { + return value switch + { + StronglyTypedIdConverter.MongoObjectIdSerializer + when backingType is not StronglyTypedIdBackingType.String and not StronglyTypedIdBackingType.NullableString => false, + _ => true + }; + } } } \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/NullableString/NullableString_MongoObjectIdSerializer.cs b/src/StronglyTypedIds/Templates/NullableString/NullableString_MongoObjectIdSerializer.cs new file mode 100644 index 000000000..e8856f3db --- /dev/null +++ b/src/StronglyTypedIds/Templates/NullableString/NullableString_MongoObjectIdSerializer.cs @@ -0,0 +1,15 @@ + class TESTIDMongoObjectIdSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override TESTID Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + var objectId = context.Reader.ReadObjectId(); + return new TESTID(objectId.ToString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, TESTID value) + { + context.Writer.WriteObjectId(string.IsNullOrWhiteSpace(value.Value) + ? MongoDB.Bson.ObjectId.GenerateNewId() + : new MongoDB.Bson.ObjectId(value.Value)); + } + } \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/String/String_MongoObjectIdSerializer.cs b/src/StronglyTypedIds/Templates/String/String_MongoObjectIdSerializer.cs new file mode 100644 index 000000000..e8856f3db --- /dev/null +++ b/src/StronglyTypedIds/Templates/String/String_MongoObjectIdSerializer.cs @@ -0,0 +1,15 @@ + class TESTIDMongoObjectIdSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override TESTID Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + var objectId = context.Reader.ReadObjectId(); + return new TESTID(objectId.ToString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, TESTID value) + { + context.Writer.WriteObjectId(string.IsNullOrWhiteSpace(value.Value) + ? MongoDB.Bson.ObjectId.GenerateNewId() + : new MongoDB.Bson.ObjectId(value.Value)); + } + } \ No newline at end of file diff --git a/test/StronglyTypedIds.IntegrationTests/StronglyTypedIds.IntegrationTests.csproj b/test/StronglyTypedIds.IntegrationTests/StronglyTypedIds.IntegrationTests.csproj index 1d6f12598..35d734b01 100644 --- a/test/StronglyTypedIds.IntegrationTests/StronglyTypedIds.IntegrationTests.csproj +++ b/test/StronglyTypedIds.IntegrationTests/StronglyTypedIds.IntegrationTests.csproj @@ -36,6 +36,7 @@ + diff --git a/test/StronglyTypedIds.IntegrationTests/Types/StringId.cs b/test/StronglyTypedIds.IntegrationTests/Types/StringId.cs index 5e7cdfa57..3f6946b66 100644 --- a/test/StronglyTypedIds.IntegrationTests/Types/StringId.cs +++ b/test/StronglyTypedIds.IntegrationTests/Types/StringId.cs @@ -34,4 +34,7 @@ public partial struct EquatableStringId { } [StronglyTypedId(backingType: StronglyTypedIdBackingType.String, implementations: StronglyTypedIdImplementations.IComparable)] public partial struct ComparableStringId { } + + [StronglyTypedId(backingType: StronglyTypedIdBackingType.String, converters: StronglyTypedIdConverter.MongoObjectIdSerializer)] + public partial struct MongoStringId { } } \ No newline at end of file From 96e24fa2baa9ddb63caaa48f15b2f1099d504f44 Mon Sep 17 00:00:00 2001 From: Marcin Rogoz Date: Mon, 4 Apr 2022 11:49:44 +0200 Subject: [PATCH 02/12] Refactored MongoSerializer and implemented it for all templates --- .../StronglyTypedIdConverter.cs | 4 ++-- ...rterAndBackingTypeCombinationDiagnostic.cs | 17 ---------------- src/StronglyTypedIds/EmbeddedSources.cs | 20 +++++++++---------- src/StronglyTypedIds/Parser.cs | 4 ---- .../SourceGenerationHelper.cs | 10 +++++----- .../StronglyTypedIdConverterExtensions.cs | 10 ---------- .../Templates/Guid/Guid_MongoSerializer.cs | 12 +++++++++++ .../Templates/Int/Int_MongoSerializer.cs | 12 +++++++++++ .../Templates/Long/Long_MongoSerializer.cs | 12 +++++++++++ .../Templates/NewId/NewId_MongoSerializer.cs | 12 +++++++++++ .../NullableString_MongoObjectIdSerializer.cs | 15 -------------- .../NullableString_MongoSerializer.cs | 19 ++++++++++++++++++ .../String/String_MongoObjectIdSerializer.cs | 15 -------------- .../String/String_MongoSerializer.cs | 12 +++++++++++ .../Types/GuidId.cs | 3 +++ .../Types/IntId.cs | 3 +++ .../Types/LongId.cs | 3 +++ .../Types/NewIdId.cs | 3 +++ .../Types/NullableStringId.cs | 3 +++ .../Types/StringId.cs | 2 +- 20 files changed, 112 insertions(+), 79 deletions(-) delete mode 100644 src/StronglyTypedIds/Diagnostics/InvalidConverterAndBackingTypeCombinationDiagnostic.cs create mode 100644 src/StronglyTypedIds/Templates/Guid/Guid_MongoSerializer.cs create mode 100644 src/StronglyTypedIds/Templates/Int/Int_MongoSerializer.cs create mode 100644 src/StronglyTypedIds/Templates/Long/Long_MongoSerializer.cs create mode 100644 src/StronglyTypedIds/Templates/NewId/NewId_MongoSerializer.cs delete mode 100644 src/StronglyTypedIds/Templates/NullableString/NullableString_MongoObjectIdSerializer.cs create mode 100644 src/StronglyTypedIds/Templates/NullableString/NullableString_MongoSerializer.cs delete mode 100644 src/StronglyTypedIds/Templates/String/String_MongoObjectIdSerializer.cs create mode 100644 src/StronglyTypedIds/Templates/String/String_MongoSerializer.cs diff --git a/src/StronglyTypedIds.Attributes/StronglyTypedIdConverter.cs b/src/StronglyTypedIds.Attributes/StronglyTypedIdConverter.cs index 3df4377fb..80f40227b 100644 --- a/src/StronglyTypedIds.Attributes/StronglyTypedIdConverter.cs +++ b/src/StronglyTypedIds.Attributes/StronglyTypedIdConverter.cs @@ -48,8 +48,8 @@ public enum StronglyTypedIdConverter DapperTypeHandler = 32, /// - /// Creates a Mongo Serializer for converting string to and from ObjectId + /// Creates a Mongo Serializer for converting string to and from type /// - MongoObjectIdSerializer = 64, + MongoSerializer = 64, } } \ No newline at end of file diff --git a/src/StronglyTypedIds/Diagnostics/InvalidConverterAndBackingTypeCombinationDiagnostic.cs b/src/StronglyTypedIds/Diagnostics/InvalidConverterAndBackingTypeCombinationDiagnostic.cs deleted file mode 100644 index 88dee4544..000000000 --- a/src/StronglyTypedIds/Diagnostics/InvalidConverterAndBackingTypeCombinationDiagnostic.cs +++ /dev/null @@ -1,17 +0,0 @@ -using Microsoft.CodeAnalysis; - -namespace StronglyTypedIds.Diagnostics -{ - internal static class InvalidConverterAndBackingTypeCombinationDiagnostic - { - internal const string Id = "STI6"; - internal const string Message = "The StronglyTypedIdBackingType and StronglyTypedIdConverter values provided are not a valid combination of flags"; - internal const string Title = "Invalid converter and backing type combination"; - - public static Diagnostic Create(SyntaxNode currentNode) => - Diagnostic.Create( - new DiagnosticDescriptor( - Id, Title, Message, category: Constants.Usage, defaultSeverity: DiagnosticSeverity.Error, isEnabledByDefault: true), - currentNode.GetLocation()); - } -} diff --git a/src/StronglyTypedIds/EmbeddedSources.cs b/src/StronglyTypedIds/EmbeddedSources.cs index 0bd6b6512..2030a0df7 100644 --- a/src/StronglyTypedIds/EmbeddedSources.cs +++ b/src/StronglyTypedIds/EmbeddedSources.cs @@ -25,7 +25,7 @@ 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"), - string.Empty, + LoadEmbeddedResource("StronglyTypedIds.Templates.Guid.Guid_MongoSerializer.cs"), false ); @@ -38,7 +38,7 @@ 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"), - string.Empty, + LoadEmbeddedResource("StronglyTypedIds.Templates.Int.Int_MongoSerializer.cs"), false ); @@ -51,7 +51,7 @@ 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"), - string.Empty, + LoadEmbeddedResource("StronglyTypedIds.Templates.Long.Long_MongoSerializer.cs"), false ); @@ -64,7 +64,7 @@ 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_MongoObjectIdSerializer.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.String.String_MongoSerializer.cs"), false ); @@ -77,7 +77,7 @@ 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_MongoObjectIdSerializer.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.NullableString.NullableString_MongoSerializer.cs"), true ); @@ -90,14 +90,14 @@ 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"), - string.Empty, + LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_MongoSerializer.cs"), false ); internal const string TypeConverterAttributeSource = " [System.ComponentModel.TypeConverter(typeof(TESTIDTypeConverter))]"; internal const string NewtonsoftJsonAttributeSource = " [Newtonsoft.Json.JsonConverter(typeof(TESTIDNewtonsoftJsonConverter))]"; internal const string SystemTextJsonAttributeSource = " [System.Text.Json.Serialization.JsonConverter(typeof(TESTIDSystemTextJsonConverter))]"; - internal const string MongoObjectIdSerializerAttributeSource = " [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(TESTIDMongoObjectIdSerializer))]"; + internal const string MongoSerializerAttributeSource = " [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(TESTIDMongoSerializer))]"; internal static string LoadEmbeddedResource(string resourceName) { @@ -123,7 +123,7 @@ public readonly struct ResourceCollection public string TypeConverter { get; } public string EfCoreValueConverter { get; } public string DapperTypeHandler { get; } - public string MongoObjectId { get; } + public string Mongo { get; } public string Comparable { get; } public ResourceCollection( @@ -135,7 +135,7 @@ public ResourceCollection( string efCoreValueConverter, string dapperTypeHandler, string comparable, - string mongoObjectId, + string mongo, bool nullableEnable) { BaseId = baseId; @@ -146,7 +146,7 @@ public ResourceCollection( DapperTypeHandler = dapperTypeHandler; Comparable = comparable; NullableEnable = nullableEnable; - MongoObjectId = mongoObjectId; + Mongo = mongo; Header = header; } } diff --git a/src/StronglyTypedIds/Parser.cs b/src/StronglyTypedIds/Parser.cs index d3b379cba..39fc96f93 100644 --- a/src/StronglyTypedIds/Parser.cs +++ b/src/StronglyTypedIds/Parser.cs @@ -195,10 +195,6 @@ public static bool IsAttributeTargetForGeneration(SyntaxNode node) { reportDiagnostic(InvalidBackingTypeDiagnostic.Create(structDeclarationSyntax)); } - else if (!converter.IsValidCombination(backingType)) - { - reportDiagnostic(InvalidConverterAndBackingTypeCombinationDiagnostic.Create(structDeclarationSyntax)); - } if (!implementations.IsValidFlags()) { diff --git a/src/StronglyTypedIds/SourceGenerationHelper.cs b/src/StronglyTypedIds/SourceGenerationHelper.cs index da6e6963f..d10f076fb 100644 --- a/src/StronglyTypedIds/SourceGenerationHelper.cs +++ b/src/StronglyTypedIds/SourceGenerationHelper.cs @@ -69,7 +69,7 @@ static string CreateId( var useSystemTextJson = converters.IsSet(StronglyTypedIdConverter.SystemTextJson); var useEfCoreValueConverter = converters.IsSet(StronglyTypedIdConverter.EfCoreValueConverter); var useDapperTypeHandler = converters.IsSet(StronglyTypedIdConverter.DapperTypeHandler); - var useMongoObjectIdSerializer = converters.IsSet(StronglyTypedIdConverter.MongoObjectIdSerializer); + var useMongoSerializer = converters.IsSet(StronglyTypedIdConverter.MongoSerializer); var useIEquatable = implementations.IsSet(StronglyTypedIdImplementations.IEquatable); var useIComparable = implementations.IsSet(StronglyTypedIdImplementations.IComparable); @@ -123,9 +123,9 @@ static string CreateId( sb.AppendLine(EmbeddedSources.TypeConverterAttributeSource); } - if (useMongoObjectIdSerializer) + if (useMongoSerializer) { - sb.AppendLine(EmbeddedSources.MongoObjectIdSerializerAttributeSource); + sb.AppendLine(EmbeddedSources.MongoSerializerAttributeSource); } sb.Append(resources.BaseId); @@ -163,9 +163,9 @@ static string CreateId( sb.AppendLine(resources.SystemTextJson); } - if (useMongoObjectIdSerializer) + if (useMongoSerializer) { - sb.AppendLine(resources.MongoObjectId); + sb.AppendLine(resources.Mongo); } sb.Replace("TESTID", idName); diff --git a/src/StronglyTypedIds/StronglyTypedIdConverterExtensions.cs b/src/StronglyTypedIds/StronglyTypedIdConverterExtensions.cs index 7a748c604..63d4174b2 100644 --- a/src/StronglyTypedIds/StronglyTypedIdConverterExtensions.cs +++ b/src/StronglyTypedIds/StronglyTypedIdConverterExtensions.cs @@ -23,15 +23,5 @@ public static bool IsValidFlags(this StronglyTypedIdImplementations value) { return (int)value >= 0 && (int)value < _maxImplementationsId; } - - public static bool IsValidCombination(this StronglyTypedIdConverter value, StronglyTypedIdBackingType backingType) - { - return value switch - { - StronglyTypedIdConverter.MongoObjectIdSerializer - when backingType is not StronglyTypedIdBackingType.String and not StronglyTypedIdBackingType.NullableString => false, - _ => true - }; - } } } \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/Guid/Guid_MongoSerializer.cs b/src/StronglyTypedIds/Templates/Guid/Guid_MongoSerializer.cs new file mode 100644 index 000000000..812a34382 --- /dev/null +++ b/src/StronglyTypedIds/Templates/Guid/Guid_MongoSerializer.cs @@ -0,0 +1,12 @@ + class TESTIDMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override TESTID Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new TESTID(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, TESTID value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/Int/Int_MongoSerializer.cs b/src/StronglyTypedIds/Templates/Int/Int_MongoSerializer.cs new file mode 100644 index 000000000..923ef3c16 --- /dev/null +++ b/src/StronglyTypedIds/Templates/Int/Int_MongoSerializer.cs @@ -0,0 +1,12 @@ + class TESTIDMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override TESTID Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new TESTID(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, TESTID value) + { + context.Writer.WriteInt32(value.Value); + } + } \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/Long/Long_MongoSerializer.cs b/src/StronglyTypedIds/Templates/Long/Long_MongoSerializer.cs new file mode 100644 index 000000000..bf5fc8c94 --- /dev/null +++ b/src/StronglyTypedIds/Templates/Long/Long_MongoSerializer.cs @@ -0,0 +1,12 @@ + class TESTIDMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override TESTID Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new TESTID(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, TESTID value) + { + context.Writer.WriteInt64(value.Value); + } + } \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/NewId/NewId_MongoSerializer.cs b/src/StronglyTypedIds/Templates/NewId/NewId_MongoSerializer.cs new file mode 100644 index 000000000..24bf91d5e --- /dev/null +++ b/src/StronglyTypedIds/Templates/NewId/NewId_MongoSerializer.cs @@ -0,0 +1,12 @@ + class TESTIDMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override TESTID Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new TESTID(MassTransit.NewId.FromGuid(new System.Guid(context.Reader.ReadString()))); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, TESTID value) + { + context.Writer.WriteString(value.Value.ToGuid().ToString()); + } + } \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/NullableString/NullableString_MongoObjectIdSerializer.cs b/src/StronglyTypedIds/Templates/NullableString/NullableString_MongoObjectIdSerializer.cs deleted file mode 100644 index e8856f3db..000000000 --- a/src/StronglyTypedIds/Templates/NullableString/NullableString_MongoObjectIdSerializer.cs +++ /dev/null @@ -1,15 +0,0 @@ - class TESTIDMongoObjectIdSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase - { - public override TESTID Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) - { - var objectId = context.Reader.ReadObjectId(); - return new TESTID(objectId.ToString()); - } - - public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, TESTID value) - { - context.Writer.WriteObjectId(string.IsNullOrWhiteSpace(value.Value) - ? MongoDB.Bson.ObjectId.GenerateNewId() - : new MongoDB.Bson.ObjectId(value.Value)); - } - } \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/NullableString/NullableString_MongoSerializer.cs b/src/StronglyTypedIds/Templates/NullableString/NullableString_MongoSerializer.cs new file mode 100644 index 000000000..0b7a8d745 --- /dev/null +++ b/src/StronglyTypedIds/Templates/NullableString/NullableString_MongoSerializer.cs @@ -0,0 +1,19 @@ + class TESTIDMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override TESTID Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new TESTID(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, TESTID value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/String/String_MongoObjectIdSerializer.cs b/src/StronglyTypedIds/Templates/String/String_MongoObjectIdSerializer.cs deleted file mode 100644 index e8856f3db..000000000 --- a/src/StronglyTypedIds/Templates/String/String_MongoObjectIdSerializer.cs +++ /dev/null @@ -1,15 +0,0 @@ - class TESTIDMongoObjectIdSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase - { - public override TESTID Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) - { - var objectId = context.Reader.ReadObjectId(); - return new TESTID(objectId.ToString()); - } - - public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, TESTID value) - { - context.Writer.WriteObjectId(string.IsNullOrWhiteSpace(value.Value) - ? MongoDB.Bson.ObjectId.GenerateNewId() - : new MongoDB.Bson.ObjectId(value.Value)); - } - } \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/String/String_MongoSerializer.cs b/src/StronglyTypedIds/Templates/String/String_MongoSerializer.cs new file mode 100644 index 000000000..af3e50267 --- /dev/null +++ b/src/StronglyTypedIds/Templates/String/String_MongoSerializer.cs @@ -0,0 +1,12 @@ + class TESTIDMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override TESTID Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new TESTID(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, TESTID value) + { + context.Writer.WriteString(value.Value); + } + } \ No newline at end of file diff --git a/test/StronglyTypedIds.IntegrationTests/Types/GuidId.cs b/test/StronglyTypedIds.IntegrationTests/Types/GuidId.cs index 9ad651d72..9450e9f01 100644 --- a/test/StronglyTypedIds.IntegrationTests/Types/GuidId.cs +++ b/test/StronglyTypedIds.IntegrationTests/Types/GuidId.cs @@ -37,4 +37,7 @@ public partial struct EquatableGuidId { } [StronglyTypedId(implementations: StronglyTypedIdImplementations.IComparable)] public partial struct ComparableGuidId { } + + [StronglyTypedId(converters: StronglyTypedIdConverter.MongoSerializer)] + public partial struct MongoGuidId { } } \ No newline at end of file diff --git a/test/StronglyTypedIds.IntegrationTests/Types/IntId.cs b/test/StronglyTypedIds.IntegrationTests/Types/IntId.cs index 15316b98a..a202e4084 100644 --- a/test/StronglyTypedIds.IntegrationTests/Types/IntId.cs +++ b/test/StronglyTypedIds.IntegrationTests/Types/IntId.cs @@ -34,4 +34,7 @@ public partial struct EquatableIntId { } [StronglyTypedId(backingType: StronglyTypedIdBackingType.Int, implementations: StronglyTypedIdImplementations.IComparable)] public partial struct ComparableIntId { } + + [StronglyTypedId(converters: StronglyTypedIdConverter.MongoSerializer, backingType: StronglyTypedIdBackingType.Int)] + public partial struct MongoIntId { } } \ No newline at end of file diff --git a/test/StronglyTypedIds.IntegrationTests/Types/LongId.cs b/test/StronglyTypedIds.IntegrationTests/Types/LongId.cs index 5a1081bb8..0a6eb0a6d 100644 --- a/test/StronglyTypedIds.IntegrationTests/Types/LongId.cs +++ b/test/StronglyTypedIds.IntegrationTests/Types/LongId.cs @@ -34,4 +34,7 @@ public partial struct EquatableLongId { } [StronglyTypedId(backingType: StronglyTypedIdBackingType.Long, implementations: StronglyTypedIdImplementations.IComparable)] public partial struct ComparableLongId { } + + [StronglyTypedId(converters: StronglyTypedIdConverter.MongoSerializer, backingType: StronglyTypedIdBackingType.Long)] + public partial struct MongoLongId { } } \ No newline at end of file diff --git a/test/StronglyTypedIds.IntegrationTests/Types/NewIdId.cs b/test/StronglyTypedIds.IntegrationTests/Types/NewIdId.cs index 0be4ce4c9..5b949c228 100644 --- a/test/StronglyTypedIds.IntegrationTests/Types/NewIdId.cs +++ b/test/StronglyTypedIds.IntegrationTests/Types/NewIdId.cs @@ -37,4 +37,7 @@ public partial struct EquatableNewIdId { } [StronglyTypedId(backingType: StronglyTypedIdBackingType.MassTransitNewId, implementations: StronglyTypedIdImplementations.IComparable)] public partial struct ComparableNewIdId { } + + [StronglyTypedId(backingType: StronglyTypedIdBackingType.MassTransitNewId, converters: StronglyTypedIdConverter.MongoSerializer)] + public partial struct MongoNewIdId { } } \ No newline at end of file diff --git a/test/StronglyTypedIds.IntegrationTests/Types/NullableStringId.cs b/test/StronglyTypedIds.IntegrationTests/Types/NullableStringId.cs index d51e901a1..dc7c8badf 100644 --- a/test/StronglyTypedIds.IntegrationTests/Types/NullableStringId.cs +++ b/test/StronglyTypedIds.IntegrationTests/Types/NullableStringId.cs @@ -34,4 +34,7 @@ public partial struct EquatableNullableStringId { } [StronglyTypedId(backingType: StronglyTypedIdBackingType.NullableString, implementations: StronglyTypedIdImplementations.IComparable)] public partial struct ComparableNullableStringId { } + + [StronglyTypedId(converters: StronglyTypedIdConverter.MongoSerializer, backingType: StronglyTypedIdBackingType.NullableString)] + public partial struct MongoNullableStringId { } } \ No newline at end of file diff --git a/test/StronglyTypedIds.IntegrationTests/Types/StringId.cs b/test/StronglyTypedIds.IntegrationTests/Types/StringId.cs index 3f6946b66..fcb9fcdff 100644 --- a/test/StronglyTypedIds.IntegrationTests/Types/StringId.cs +++ b/test/StronglyTypedIds.IntegrationTests/Types/StringId.cs @@ -35,6 +35,6 @@ public partial struct EquatableStringId { } [StronglyTypedId(backingType: StronglyTypedIdBackingType.String, implementations: StronglyTypedIdImplementations.IComparable)] public partial struct ComparableStringId { } - [StronglyTypedId(backingType: StronglyTypedIdBackingType.String, converters: StronglyTypedIdConverter.MongoObjectIdSerializer)] + [StronglyTypedId(backingType: StronglyTypedIdBackingType.String, converters: StronglyTypedIdConverter.MongoSerializer)] public partial struct MongoStringId { } } \ No newline at end of file From 88be6386134c22d75e03ff0f963e0e7c0c6af0cd Mon Sep 17 00:00:00 2001 From: Marcin Rogoz Date: Mon, 4 Apr 2022 13:08:04 +0200 Subject: [PATCH 03/12] Implemented ObjectId backing type. --- .../StronglyTypedIdBackingType.cs | 1 + src/StronglyTypedIds/EmbeddedSources.cs | 13 ++++++ .../SourceGenerationHelper.cs | 1 + .../Templates/ObjectId/ObjectId_Base.cs | 24 +++++++++++ .../ObjectId/ObjectId_DapperTypeHandler.cs | 17 ++++++++ .../ObjectId/ObjectId_EfCoreValueConverter.cs | 11 +++++ .../ObjectId/ObjectId_IComparable.cs | 1 + .../ObjectId/ObjectId_MongoSerializer.cs | 12 ++++++ .../ObjectId_NewtonsoftJsonConverter.cs | 19 +++++++++ .../ObjectId_SystemTextJsonConverter.cs | 13 ++++++ .../ObjectId/ObjectId_TypeConverter.cs | 42 +++++++++++++++++++ .../Types/ObjectId.cs | 37 ++++++++++++++++ 12 files changed, 191 insertions(+) create mode 100644 src/StronglyTypedIds/Templates/ObjectId/ObjectId_Base.cs create mode 100644 src/StronglyTypedIds/Templates/ObjectId/ObjectId_DapperTypeHandler.cs create mode 100644 src/StronglyTypedIds/Templates/ObjectId/ObjectId_EfCoreValueConverter.cs create mode 100644 src/StronglyTypedIds/Templates/ObjectId/ObjectId_IComparable.cs create mode 100644 src/StronglyTypedIds/Templates/ObjectId/ObjectId_MongoSerializer.cs create mode 100644 src/StronglyTypedIds/Templates/ObjectId/ObjectId_NewtonsoftJsonConverter.cs create mode 100644 src/StronglyTypedIds/Templates/ObjectId/ObjectId_SystemTextJsonConverter.cs create mode 100644 src/StronglyTypedIds/Templates/ObjectId/ObjectId_TypeConverter.cs create mode 100644 test/StronglyTypedIds.IntegrationTests/Types/ObjectId.cs diff --git a/src/StronglyTypedIds.Attributes/StronglyTypedIdBackingType.cs b/src/StronglyTypedIds.Attributes/StronglyTypedIdBackingType.cs index 0016511b6..be78594f2 100644 --- a/src/StronglyTypedIds.Attributes/StronglyTypedIdBackingType.cs +++ b/src/StronglyTypedIds.Attributes/StronglyTypedIdBackingType.cs @@ -18,5 +18,6 @@ public enum StronglyTypedIdBackingType Long = 4, NullableString = 5, MassTransitNewId = 6, + ObjectId = 7 } } \ No newline at end of file diff --git a/src/StronglyTypedIds/EmbeddedSources.cs b/src/StronglyTypedIds/EmbeddedSources.cs index 2030a0df7..e9df74496 100644 --- a/src/StronglyTypedIds/EmbeddedSources.cs +++ b/src/StronglyTypedIds/EmbeddedSources.cs @@ -93,6 +93,19 @@ internal static class EmbeddedSources LoadEmbeddedResource("StronglyTypedIds.Templates.NewId.NewId_MongoSerializer.cs"), false ); + + internal static readonly ResourceCollection ObjectIdResources = new( + AutoGeneratedHeader, + LoadEmbeddedResource("StronglyTypedIds.Templates.ObjectId.ObjectId_Base.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.ObjectId.ObjectId_NewtonsoftJsonConverter.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.ObjectId.ObjectId_SystemTextJsonConverter.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.ObjectId.ObjectId_TypeConverter.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.ObjectId.ObjectId_EfCoreValueConverter.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.ObjectId.ObjectId_DapperTypeHandler.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.ObjectId.ObjectId_IComparable.cs"), + LoadEmbeddedResource("StronglyTypedIds.Templates.ObjectId.ObjectId_MongoSerializer.cs"), + false + ); internal const string TypeConverterAttributeSource = " [System.ComponentModel.TypeConverter(typeof(TESTIDTypeConverter))]"; internal const string NewtonsoftJsonAttributeSource = " [Newtonsoft.Json.JsonConverter(typeof(TESTIDNewtonsoftJsonConverter))]"; diff --git a/src/StronglyTypedIds/SourceGenerationHelper.cs b/src/StronglyTypedIds/SourceGenerationHelper.cs index d10f076fb..071b41e45 100644 --- a/src/StronglyTypedIds/SourceGenerationHelper.cs +++ b/src/StronglyTypedIds/SourceGenerationHelper.cs @@ -32,6 +32,7 @@ public static string CreateId( StronglyTypedIdBackingType.String => EmbeddedSources.StringResources, StronglyTypedIdBackingType.NullableString => EmbeddedSources.NullableStringResources, StronglyTypedIdBackingType.MassTransitNewId => EmbeddedSources.NewIdResources, + StronglyTypedIdBackingType.ObjectId => EmbeddedSources.ObjectIdResources, _ => throw new ArgumentException("Unknown backing type: " + backingType, nameof(backingType)), }; diff --git a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_Base.cs b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_Base.cs new file mode 100644 index 000000000..b56cf963d --- /dev/null +++ b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_Base.cs @@ -0,0 +1,24 @@ + readonly partial struct TESTID : INTERFACES + { + public MongoDB.Bson.ObjectId Value { get; } + + public TESTID(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static TESTID New() => new TESTID(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly TESTID Empty = new TESTID(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(TESTID other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is TESTID other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(TESTID a, TESTID b) => a.Equals(b); + public static bool operator !=(TESTID a, TESTID b) => !(a == b); \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_DapperTypeHandler.cs b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_DapperTypeHandler.cs new file mode 100644 index 000000000..a641c2a1c --- /dev/null +++ b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_DapperTypeHandler.cs @@ -0,0 +1,17 @@ + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, TESTID value) + { + parameter.Value = value.Value.ToString(); + } + + public override TESTID Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new TESTID(new MongoDB.Bson.ObjectId(stringValue)), + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to TESTID"), + }; + } + } \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_EfCoreValueConverter.cs b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_EfCoreValueConverter.cs new file mode 100644 index 000000000..c94a00c6c --- /dev/null +++ b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_EfCoreValueConverter.cs @@ -0,0 +1,11 @@ + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value.ToString(), + value => new TESTID(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_IComparable.cs b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_IComparable.cs new file mode 100644 index 000000000..e0696ee2a --- /dev/null +++ b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_IComparable.cs @@ -0,0 +1 @@ + public int CompareTo(TESTID other) => Value.CompareTo(other.Value); \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_MongoSerializer.cs b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_MongoSerializer.cs new file mode 100644 index 000000000..63810fa83 --- /dev/null +++ b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_MongoSerializer.cs @@ -0,0 +1,12 @@ + class TESTIDMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override TESTID Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new TESTID(context.Reader.ReadObjectId()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, TESTID value) + { + context.Writer.WriteObjectId(value.Value); + } + } \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_NewtonsoftJsonConverter.cs b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_NewtonsoftJsonConverter.cs new file mode 100644 index 000000000..ac741e64a --- /dev/null +++ b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_NewtonsoftJsonConverter.cs @@ -0,0 +1,19 @@ + + class TESTIDNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(TESTID); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (TESTID)value; + serializer.Serialize(writer, id.Value.ToString()); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + return new TESTID(new MongoDB.Bson.ObjectId(serializer.Deserialize(reader))); + } + } \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_SystemTextJsonConverter.cs b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_SystemTextJsonConverter.cs new file mode 100644 index 000000000..c15f46d38 --- /dev/null +++ b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_SystemTextJsonConverter.cs @@ -0,0 +1,13 @@ + + class TESTIDSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override TESTID Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + return new TESTID(new MongoDB.Bson.ObjectId(reader.GetString())); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, TESTID value, System.Text.Json.JsonSerializerOptions options) + { + writer.WriteStringValue(value.Value.ToString()); + } + } \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_TypeConverter.cs b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_TypeConverter.cs new file mode 100644 index 000000000..0ff4a7c57 --- /dev/null +++ b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_TypeConverter.cs @@ -0,0 +1,42 @@ + + class TESTIDTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new TESTID(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new TESTID(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is TESTID idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } \ No newline at end of file diff --git a/test/StronglyTypedIds.IntegrationTests/Types/ObjectId.cs b/test/StronglyTypedIds.IntegrationTests/Types/ObjectId.cs new file mode 100644 index 000000000..caa671796 --- /dev/null +++ b/test/StronglyTypedIds.IntegrationTests/Types/ObjectId.cs @@ -0,0 +1,37 @@ +namespace StronglyTypedIds.IntegrationTests.Types; + +[StronglyTypedId(backingType: StronglyTypedIdBackingType.ObjectId)] +partial struct ObjectIdId { } + +[StronglyTypedId(backingType: StronglyTypedIdBackingType.ObjectId, converters: StronglyTypedIdConverter.None)] +public partial struct NoConverterObjectIdId { } + +[StronglyTypedId(backingType: StronglyTypedIdBackingType.ObjectId, converters: StronglyTypedIdConverter.TypeConverter)] +public partial struct NoJsonObjectIdId { } + +[StronglyTypedId(backingType: StronglyTypedIdBackingType.ObjectId, converters: StronglyTypedIdConverter.NewtonsoftJson)] +public partial struct NewtonsoftJsonObjectIdId { } + +[StronglyTypedId(backingType: StronglyTypedIdBackingType.ObjectId, converters: StronglyTypedIdConverter.SystemTextJson)] +public partial struct SystemTextJsonObjectIdId { } + +[StronglyTypedId(backingType: StronglyTypedIdBackingType.ObjectId, converters: StronglyTypedIdConverter.NewtonsoftJson | StronglyTypedIdConverter.SystemTextJson)] +public partial struct BothJsonObjectIdId { } + +[StronglyTypedId(backingType: StronglyTypedIdBackingType.ObjectId, converters: StronglyTypedIdConverter.EfCoreValueConverter)] +public partial struct EfCoreObjectIdId { } + +[StronglyTypedId(backingType: StronglyTypedIdBackingType.ObjectId, converters: StronglyTypedIdConverter.DapperTypeHandler)] +public partial struct DapperObjectIdId { } + +[StronglyTypedId(backingType: StronglyTypedIdBackingType.ObjectId, implementations: StronglyTypedIdImplementations.IEquatable | StronglyTypedIdImplementations.IComparable)] +public partial struct BothObjectIdId { } + +[StronglyTypedId(backingType: StronglyTypedIdBackingType.ObjectId, implementations: StronglyTypedIdImplementations.IEquatable)] +public partial struct EquatableObjectIdId { } + +[StronglyTypedId(backingType: StronglyTypedIdBackingType.ObjectId, implementations: StronglyTypedIdImplementations.IComparable)] +public partial struct ComparableObjectIdId { } + +[StronglyTypedId(backingType: StronglyTypedIdBackingType.ObjectId, converters: StronglyTypedIdConverter.MongoSerializer)] +public partial struct MongoObjectIdId { } \ No newline at end of file From 2d2b2e02f81fc695e86aee76a98712743faf82b1 Mon Sep 17 00:00:00 2001 From: Marcin Rogoz Date: Fri, 6 May 2022 23:33:47 +0200 Subject: [PATCH 04/12] Added some integration tests for ObjectId backing type. --- .../ObjectId_NewtonsoftJsonConverter.cs | 3 +- .../ObjectId_SystemTextJsonConverter.cs | 3 +- .../DapperTypeHandlers.cs | 1 + .../ObjectIdTests.cs | 360 ++++++++++++++++++ .../Types/ObjectId.cs | 5 +- ...s.Nuget.Attributes.IntegrationTests.csproj | 1 + ...nglyTypedIds.Nuget.IntegrationTests.csproj | 1 + 7 files changed, 371 insertions(+), 3 deletions(-) create mode 100644 test/StronglyTypedIds.IntegrationTests/ObjectIdTests.cs diff --git a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_NewtonsoftJsonConverter.cs b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_NewtonsoftJsonConverter.cs index ac741e64a..332aa6322 100644 --- a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_NewtonsoftJsonConverter.cs +++ b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_NewtonsoftJsonConverter.cs @@ -14,6 +14,7 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { - return new TESTID(new MongoDB.Bson.ObjectId(serializer.Deserialize(reader))); + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? null : new TESTID(new MongoDB.Bson.ObjectId(result)); } } \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_SystemTextJsonConverter.cs b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_SystemTextJsonConverter.cs index c15f46d38..28c740db0 100644 --- a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_SystemTextJsonConverter.cs +++ b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_SystemTextJsonConverter.cs @@ -3,7 +3,8 @@ class TESTIDSystemTextJsonConverter : System.Text.Json.Serialization.JsonConvert { public override TESTID Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - return new TESTID(new MongoDB.Bson.ObjectId(reader.GetString())); + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? TESTID.Empty : new TESTID(new MongoDB.Bson.ObjectId(result)); } public override void Write(System.Text.Json.Utf8JsonWriter writer, TESTID value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.IntegrationTests/DapperTypeHandlers.cs b/test/StronglyTypedIds.IntegrationTests/DapperTypeHandlers.cs index 0ce1ce679..b55f4227a 100644 --- a/test/StronglyTypedIds.IntegrationTests/DapperTypeHandlers.cs +++ b/test/StronglyTypedIds.IntegrationTests/DapperTypeHandlers.cs @@ -15,6 +15,7 @@ public static void AddHandlers() SqlMapper.AddTypeHandler(new DapperLongId.DapperTypeHandler()); SqlMapper.AddTypeHandler(new DapperNullableStringId.DapperTypeHandler()); SqlMapper.AddTypeHandler(new DapperNewIdId.DapperTypeHandler()); + SqlMapper.AddTypeHandler(new DapperObjectIdId.DapperTypeHandler()); } } } \ No newline at end of file diff --git a/test/StronglyTypedIds.IntegrationTests/ObjectIdTests.cs b/test/StronglyTypedIds.IntegrationTests/ObjectIdTests.cs new file mode 100644 index 000000000..5b1332bc5 --- /dev/null +++ b/test/StronglyTypedIds.IntegrationTests/ObjectIdTests.cs @@ -0,0 +1,360 @@ +using System; +using System.ComponentModel; +using System.Linq; +using System.Threading.Tasks; +using Dapper; +using Microsoft.Data.Sqlite; +using Microsoft.EntityFrameworkCore; +using MongoDB.Bson; +using StronglyTypedIds.IntegrationTests.Types; +using Xunit; +using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; +using SystemTextJsonSerializer = System.Text.Json.JsonSerializer; + +namespace StronglyTypedIds.IntegrationTests; + +public class ObjectIdTests +{ + [Fact] + public void SameValuesAreEqual() + { + var id = ObjectId.GenerateNewId(); + var foo1 = new ObjectIdId1(id); + var foo2 = new ObjectIdId1(id); + + Assert.Equal(foo1, foo2); + } + + [Fact] + public void EmptyValueIsEmpty() + { + Assert.Equal(ObjectIdId1.Empty.Value, ObjectId.Empty); + } + + + [Fact] + public void DifferentValuesAreUnequal() + { + var foo1 = ObjectIdId1.New(); + var foo2 = ObjectIdId1.New(); + + Assert.NotEqual(foo1, foo2); + } + + [Fact] + public void OverloadsWorkCorrectly() + { + var id = ObjectId.GenerateNewId(); + var same1 = new ObjectIdId1(id); + var same2 = new ObjectIdId1(id); + var different = ObjectIdId1.New(); + + Assert.True(same1 == same2); + Assert.False(same1 == different); + Assert.False(same1 != same2); + Assert.True(same1 != different); + } + + [Fact] + public void DifferentTypesAreUnequal() + { + var bar = ObjectIdId2.New(); + var foo = ObjectIdId1.New(); + + //Assert.NotEqual(bar, foo); // does not compile + Assert.NotEqual((object)bar, (object)foo); + } + + [Fact] + public void CantCreateEmptyGeneratedId1() + { + var foo = new ObjectIdId1(); + var bar = new ObjectIdId2(); + + //Assert.NotEqual(bar, foo); // does not compile + Assert.NotEqual((object)bar, (object)foo); + } + + [Fact] + public void CanSerializeToObjectId_WithTypeConverter() + { + var foo = NewtonsoftJsonObjectIdId.New(); + + var serializedFoo = NewtonsoftJsonSerializer.SerializeObject(foo); + var serializedObjectId = NewtonsoftJsonSerializer.SerializeObject(foo.Value.ToString()); + + Assert.Equal(serializedFoo, serializedObjectId); + } + + [Fact] + public void CanSerializeToObjectId_WithSystemTextJsonProvider() + { + var foo = SystemTextJsonObjectIdId.New(); + + var serializedFoo = SystemTextJsonSerializer.Serialize(foo); + var serializedObjectId = SystemTextJsonSerializer.Serialize(foo.Value.ToString()); + + Assert.Equal(serializedFoo, serializedObjectId); + } + + [Fact] + public void CanDeserializeFromObjectId_WithNewtonsoftJsonProvider() + { + var value = ObjectId.GenerateNewId(); + var foo = new NewtonsoftJsonObjectIdId(value); + var serializedObjectId = NewtonsoftJsonSerializer.SerializeObject(value); + + var deserializedFoo = NewtonsoftJsonSerializer.DeserializeObject(serializedObjectId); + + Assert.Equal(foo, deserializedFoo); + } + + [Fact] + public void CanSerializeToNullable_WithNewtonsoftJsonProvider() + { + var entity = new EntityWithNullableId { Id = null }; + + var json = NewtonsoftJsonSerializer.SerializeObject(entity); + var deserialize = NewtonsoftJsonSerializer.DeserializeObject(json); + + Assert.NotNull(deserialize); + Assert.Null(deserialize.Id); + } + + [Fact] + public void CanDeserializeFromObjectId_WithSystemTextJsonProvider() + { + var value = ObjectId.GenerateNewId(); + var foo = new SystemTextJsonObjectIdId(value); + var serializedObjectId = SystemTextJsonSerializer.Serialize(value.ToString()); + + var deserializedFoo = SystemTextJsonSerializer.Deserialize(serializedObjectId); + + Assert.Equal(foo, deserializedFoo); + } + + [Fact] + public void CanSerializeToObjectId_WithBothJsonConverters() + { + var foo = BothJsonObjectIdId.New(); + + var serializedFoo1 = NewtonsoftJsonSerializer.SerializeObject(foo); + var serializedObjectId1 = NewtonsoftJsonSerializer.SerializeObject(foo.Value.ToString()); + + var serializedFoo2 = SystemTextJsonSerializer.Serialize(foo); + var serializedObjectId2 = SystemTextJsonSerializer.Serialize(foo.Value.ToString()); + + Assert.Equal(serializedFoo1, serializedObjectId1); + Assert.Equal(serializedFoo2, serializedObjectId2); + } + + [Fact] + public void WhenNoJsonConverter_SystemTextJsonSerializesWithValueProperty() + { + var foo = NoJsonObjectIdId.New(); + + var serialized = SystemTextJsonSerializer.Serialize(foo); + + var expected = "{\"Value\":" + SystemTextJsonSerializer.Serialize(foo.Value) + "}"; + + Assert.Equal(expected, serialized); + } + + [Fact] + public void WhenNoJsonConverter_NewtonsoftSerializesWithoutValueProperty() + { + var foo = NoJsonObjectIdId.New(); + + var serialized = NewtonsoftJsonSerializer.SerializeObject(foo); + + var expected = $"\"{foo.Value.ToString()}\""; + + Assert.Equal(expected, serialized); + } + + [Fact] + public void WhenNoTypeConverter_SerializesWithValueProperty() + { + var foo = NoConverterObjectIdId.New(); + + var newtonsoft = SystemTextJsonSerializer.Serialize(foo); + var systemText = SystemTextJsonSerializer.Serialize(foo); + + var expected = "{\"Value\":" + SystemTextJsonSerializer.Serialize(foo.Value) + "}"; + + Assert.Equal(expected, newtonsoft); + Assert.Equal(expected, systemText); + } + + [Fact] + public void WhenEfCoreValueConverterUsesValueConverter() + { + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + + var options = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + + using (var context = new TestDbContext(options)) + { + context.Database.EnsureCreated(); + context.Entities.Add( + new TestEntity { Id = EfCoreObjectIdId.New() }); + context.SaveChanges(); + } + using (var context = new TestDbContext(options)) + { + var all = context.Entities.ToList(); + Assert.Single(all); + } + } + + [Fact] + public async Task WhenDapperValueConverterUsesValueConverter() + { + using var connection = new SqliteConnection("DataSource=:memory:"); + await connection.OpenAsync(); + + var results = await connection.QueryAsync("SELECT '62758c6ee39f6ef4751bb831'"); + + var value = Assert.Single(results); + Assert.Equal(new DapperObjectIdId(new ObjectId("62758c6ee39f6ef4751bb831")), value); + } + + [Theory] + [InlineData("62758cacb6f910fe91bd697b")] + public void TypeConverter_CanConvertToAndFrom(string value) + { + var converter = TypeDescriptor.GetConverter(typeof(NoJsonObjectIdId)); + var id = converter.ConvertFrom(value); + Assert.IsType(id); + Assert.Equal(new NoJsonObjectIdId(new ObjectId(value)), id); + + var reconverted = converter.ConvertTo(id, value.GetType()); + Assert.Equal(value, reconverted); + } + + [Fact] + public void CanCompareDefaults() + { + ComparableObjectIdId original = default; + var other = ComparableObjectIdId.Empty; + + var compare1 = original.CompareTo(other); + var compare2 = other.CompareTo(original); + Assert.Equal(compare1, -compare2); + } + + [Fact] + public void CanEquateDefaults() + { + EquatableObjectIdId original = default; + var other = EquatableObjectIdId.Empty; + + var equals1 = (original as IEquatable).Equals(other); + var equals2 = (other as IEquatable).Equals(original); + + Assert.Equal(equals1, equals2); + } + + [Fact] + public void ImplementsInterfaces() + { + Assert.IsAssignableFrom>(BothObjectIdId.Empty); + Assert.IsAssignableFrom>(BothObjectIdId.Empty); + + Assert.IsAssignableFrom>(EquatableObjectIdId.Empty); + Assert.IsAssignableFrom>(ComparableObjectIdId.Empty); + +#pragma warning disable 184 + Assert.False(ObjectIdId1.Empty is IComparable); + Assert.False(ObjectIdId1.Empty is IEquatable); +#pragma warning restore 184 + } + + +#if NET6_0_OR_GREATER + [Fact] + public void WhenConventionBasedEfCoreValueConverterUsesValueConverter() + { + var connection = new SqliteConnection("DataSource=:memory:"); + connection.Open(); + + var options = new DbContextOptionsBuilder() + .UseSqlite(connection) + .Options; + + using (var context = new ConventionsDbContext(options)) + { + context.Database.EnsureCreated(); + context.Entities.Add( + new TestEntity { Id = EfCoreObjectIdId.New() }); + context.SaveChanges(); + } + using (var context = new ConventionsDbContext(options)) + { + var all = context.Entities.ToList(); + Assert.Single(all); + } + } + + public class ConventionsDbContext : DbContext + { + public DbSet Entities { get; set; } + + public ConventionsDbContext(DbContextOptions options) : base(options) + { + } + + protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder) + { + configurationBuilder + .Properties() + .HaveConversion(); + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder + .Entity(builder => + { + builder + .Property(x => x.Id) + .ValueGeneratedNever(); + }); + } + } +#endif + + public class TestDbContext : DbContext + { + public DbSet Entities { get; set; } + + public TestDbContext(DbContextOptions options) : base(options) + { + } + + protected override void OnModelCreating(ModelBuilder modelBuilder) + { + modelBuilder + .Entity(builder => + { + builder + .Property(x => x.Id) + .HasConversion(new EfCoreObjectIdId.EfCoreValueConverter()) + .ValueGeneratedNever(); + }); + } + } + + public class TestEntity + { + public EfCoreObjectIdId Id { get; set; } + } + + public class EntityWithNullableId + { + public NewtonsoftJsonObjectIdId? Id { get; set; } + } +} \ No newline at end of file diff --git a/test/StronglyTypedIds.IntegrationTests/Types/ObjectId.cs b/test/StronglyTypedIds.IntegrationTests/Types/ObjectId.cs index caa671796..5bf40ca47 100644 --- a/test/StronglyTypedIds.IntegrationTests/Types/ObjectId.cs +++ b/test/StronglyTypedIds.IntegrationTests/Types/ObjectId.cs @@ -1,7 +1,10 @@ namespace StronglyTypedIds.IntegrationTests.Types; [StronglyTypedId(backingType: StronglyTypedIdBackingType.ObjectId)] -partial struct ObjectIdId { } +partial struct ObjectIdId1 { } + +[StronglyTypedId(backingType: StronglyTypedIdBackingType.ObjectId)] +partial struct ObjectIdId2 { } [StronglyTypedId(backingType: StronglyTypedIdBackingType.ObjectId, converters: StronglyTypedIdConverter.None)] public partial struct NoConverterObjectIdId { } diff --git a/test/StronglyTypedIds.Nuget.Attributes.IntegrationTests/StronglyTypedIds.Nuget.Attributes.IntegrationTests.csproj b/test/StronglyTypedIds.Nuget.Attributes.IntegrationTests/StronglyTypedIds.Nuget.Attributes.IntegrationTests.csproj index bb760958b..819f72b9c 100644 --- a/test/StronglyTypedIds.Nuget.Attributes.IntegrationTests/StronglyTypedIds.Nuget.Attributes.IntegrationTests.csproj +++ b/test/StronglyTypedIds.Nuget.Attributes.IntegrationTests/StronglyTypedIds.Nuget.Attributes.IntegrationTests.csproj @@ -39,6 +39,7 @@ + diff --git a/test/StronglyTypedIds.Nuget.IntegrationTests/StronglyTypedIds.Nuget.IntegrationTests.csproj b/test/StronglyTypedIds.Nuget.IntegrationTests/StronglyTypedIds.Nuget.IntegrationTests.csproj index d79579881..1930dbe2f 100644 --- a/test/StronglyTypedIds.Nuget.IntegrationTests/StronglyTypedIds.Nuget.IntegrationTests.csproj +++ b/test/StronglyTypedIds.Nuget.IntegrationTests/StronglyTypedIds.Nuget.IntegrationTests.csproj @@ -38,6 +38,7 @@ + From 608aae844cab67c807ba55e97cc856bdbe1d777a Mon Sep 17 00:00:00 2001 From: Marcin Rogoz Date: Mon, 9 May 2022 13:53:46 +0200 Subject: [PATCH 05/12] Implemented MongoSerializer tests for all backing types --- .../Fixtures/MongoDbFixture.cs | 31 +++++++++++++++++++ .../GuidIdTests.cs | 28 +++++++++++++++++ .../IntIdTests.cs | 27 ++++++++++++++++ .../LongIdTests.cs | 27 ++++++++++++++++ .../MassTransitNewIdTests.cs | 28 +++++++++++++++++ .../NullableStringIdTests.cs | 27 ++++++++++++++++ .../ObjectIdTests.cs | 28 +++++++++++++++++ .../StringIdTests.cs | 27 ++++++++++++++++ .../StronglyTypedIds.IntegrationTests.csproj | 7 ++++- ...s.Nuget.Attributes.IntegrationTests.csproj | 6 +++- ...nglyTypedIds.Nuget.IntegrationTests.csproj | 6 +++- 11 files changed, 239 insertions(+), 3 deletions(-) create mode 100644 test/StronglyTypedIds.IntegrationTests/Fixtures/MongoDbFixture.cs diff --git a/test/StronglyTypedIds.IntegrationTests/Fixtures/MongoDbFixture.cs b/test/StronglyTypedIds.IntegrationTests/Fixtures/MongoDbFixture.cs new file mode 100644 index 000000000..58e4f1c29 --- /dev/null +++ b/test/StronglyTypedIds.IntegrationTests/Fixtures/MongoDbFixture.cs @@ -0,0 +1,31 @@ +using System; +using Mongo2Go; +using MongoDB.Driver; +using Xunit; + +namespace StronglyTypedIds.IntegrationTests.Fixtures; + +public class MongoDbFixture : IDisposable +{ + private readonly MongoDbRunner _runner; + + public MongoDbFixture() + { + _runner = MongoDbRunner.Start(); + var client = new MongoClient(_runner.ConnectionString); + Database = client.GetDatabase("IntegrationTest"); + } + + public IMongoDatabase Database { get; } + + public void Dispose() + { + _runner?.Dispose(); + } +} + +[CollectionDefinition(Name)] +public class MongoDbCollection : ICollectionFixture +{ + public const string Name = "MongoDB collection"; +} \ No newline at end of file diff --git a/test/StronglyTypedIds.IntegrationTests/GuidIdTests.cs b/test/StronglyTypedIds.IntegrationTests/GuidIdTests.cs index 2905f70e6..537849ca8 100644 --- a/test/StronglyTypedIds.IntegrationTests/GuidIdTests.cs +++ b/test/StronglyTypedIds.IntegrationTests/GuidIdTests.cs @@ -5,6 +5,8 @@ using Dapper; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; +using MongoDB.Driver; +using StronglyTypedIds.IntegrationTests.Fixtures; using StronglyTypedIds.IntegrationTests.Types; using Xunit; using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; @@ -12,8 +14,16 @@ namespace StronglyTypedIds.IntegrationTests { + [Collection(MongoDbCollection.Name)] public class GuidIdTests { + private readonly MongoDbFixture _mongoDbFixture; + + public GuidIdTests(MongoDbFixture mongoDbFixture) + { + _mongoDbFixture = mongoDbFixture; + } + [Fact] public void SameValuesAreEqual() { @@ -220,6 +230,19 @@ public async Task WhenDapperValueConverterUsesValueConverter() var value = Assert.Single(results); Assert.Equal(value, new DapperGuidId(Guid.Parse("5640dad4-862a-4738-9e3c-c76dc227eb66"))); } + + [Fact] + public async Task WhenMongoSerializerUsesSerializer() + { + var collection = _mongoDbFixture.Database.GetCollection("GuidIdTestCollection"); + + var guidValue = Guid.NewGuid(); + var original = new TestDocument { Id = new MongoGuidId(guidValue) }; + await collection.InsertOneAsync(original); + var retrieved = await collection.Find(x => x.Id == new MongoGuidId(guidValue)).FirstAsync(); + + Assert.Equal(new MongoGuidId(guidValue), retrieved.Id); + } [Theory] [InlineData("78104553-f1cd-41ec-bcb6-d3a8ff8d994d")] @@ -355,5 +378,10 @@ public class EntityWithNullableId { public NewtonsoftJsonGuidId? Id { get; set; } } + + public class TestDocument + { + public MongoGuidId Id { get; set; } + } } } \ No newline at end of file diff --git a/test/StronglyTypedIds.IntegrationTests/IntIdTests.cs b/test/StronglyTypedIds.IntegrationTests/IntIdTests.cs index e2c48be64..57a30e48b 100644 --- a/test/StronglyTypedIds.IntegrationTests/IntIdTests.cs +++ b/test/StronglyTypedIds.IntegrationTests/IntIdTests.cs @@ -5,6 +5,8 @@ using Dapper; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; +using MongoDB.Driver; +using StronglyTypedIds.IntegrationTests.Fixtures; using StronglyTypedIds.IntegrationTests.Types; using Xunit; using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; @@ -12,8 +14,16 @@ namespace StronglyTypedIds.IntegrationTests { + [Collection(MongoDbCollection.Name)] public class IntIdTests { + private readonly MongoDbFixture _mongoDbFixture; + + public IntIdTests(MongoDbFixture mongoDbFixture) + { + _mongoDbFixture = mongoDbFixture; + } + [Fact] public void SameValuesAreEqual() { @@ -211,6 +221,18 @@ public async Task WhenDapperValueConverterUsesValueConverter() var value = Assert.Single(results); Assert.Equal(new DapperIntId(123), value); } + + [Fact] + public async Task WhenMongoSerializerUsesSerializer() + { + var collection = _mongoDbFixture.Database.GetCollection("IntIdTestCollection"); + + var original = new TestDocument { Id = new MongoIntId(123) }; + await collection.InsertOneAsync(original); + var retrieved = await collection.Find(x => x.Id == new MongoIntId(123)).FirstAsync(); + + Assert.Equal(new MongoIntId(123), retrieved.Id); + } [Theory] [InlineData(123)] @@ -348,5 +370,10 @@ public class EntityWithNullableId { public NewtonsoftJsonIntId? Id { get; set; } } + + public class TestDocument + { + public MongoIntId Id { get; set; } + } } } \ No newline at end of file diff --git a/test/StronglyTypedIds.IntegrationTests/LongIdTests.cs b/test/StronglyTypedIds.IntegrationTests/LongIdTests.cs index b2f70dbcb..6496ff33e 100644 --- a/test/StronglyTypedIds.IntegrationTests/LongIdTests.cs +++ b/test/StronglyTypedIds.IntegrationTests/LongIdTests.cs @@ -5,6 +5,8 @@ using Dapper; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; +using MongoDB.Driver; +using StronglyTypedIds.IntegrationTests.Fixtures; using StronglyTypedIds.IntegrationTests.Types; using Xunit; using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; @@ -12,8 +14,16 @@ namespace StronglyTypedIds.IntegrationTests { + [Collection(MongoDbCollection.Name)] public class LongIdTests { + private readonly MongoDbFixture _mongoDbFixture; + + public LongIdTests(MongoDbFixture mongoDbFixture) + { + _mongoDbFixture = mongoDbFixture; + } + [Fact] public void SameValuesAreEqual() { @@ -212,6 +222,18 @@ public async Task WhenDapperValueConverterUsesValueConverter() var value = Assert.Single(results); Assert.Equal(value, new DapperLongId(123)); } + + [Fact] + public async Task WhenMongoSerializerUsesSerializer() + { + var collection = _mongoDbFixture.Database.GetCollection("LongIdTestCollection"); + + var original = new TestDocument { Id = new MongoLongId(123) }; + await collection.InsertOneAsync(original); + var retrieved = await collection.Find(x => x.Id == new MongoLongId(123)).FirstAsync(); + + Assert.Equal(new MongoLongId(123), retrieved.Id); + } [Theory] [InlineData(123L)] @@ -349,5 +371,10 @@ public class EntityWithNullableId { public NewtonsoftJsonLongId? Id { get; set; } } + + public class TestDocument + { + public MongoLongId Id { get; set; } + } } } \ No newline at end of file diff --git a/test/StronglyTypedIds.IntegrationTests/MassTransitNewIdTests.cs b/test/StronglyTypedIds.IntegrationTests/MassTransitNewIdTests.cs index e6dce6d74..300c4fa56 100644 --- a/test/StronglyTypedIds.IntegrationTests/MassTransitNewIdTests.cs +++ b/test/StronglyTypedIds.IntegrationTests/MassTransitNewIdTests.cs @@ -6,6 +6,8 @@ using MassTransit; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; +using MongoDB.Driver; +using StronglyTypedIds.IntegrationTests.Fixtures; using StronglyTypedIds.IntegrationTests.Types; using Xunit; using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; @@ -13,8 +15,16 @@ namespace StronglyTypedIds.IntegrationTests { + [Collection(MongoDbCollection.Name)] public class MassTransitNewIdTests { + private readonly MongoDbFixture _mongoDbFixture; + + public MassTransitNewIdTests(MongoDbFixture mongoDbFixture) + { + _mongoDbFixture = mongoDbFixture; + } + [Fact] public void SameValuesAreEqual() { @@ -221,6 +231,19 @@ public async Task WhenDapperValueConverterUsesValueConverter() var value = Assert.Single(results); Assert.Equal(new DapperNewIdId(NewId.FromGuid(Guid.Parse("5640dad4-862a-4738-9e3c-c76dc227eb66"))), value); } + + [Fact] + public async Task WhenMongoSerializerUsesSerializer() + { + var collection = _mongoDbFixture.Database.GetCollection("NewIdTestCollection"); + + var guidValue = Guid.NewGuid(); + var original = new TestDocument { Id = new MongoNewIdId(NewId.FromGuid(guidValue)) }; + await collection.InsertOneAsync(original); + var retrieved = await collection.Find(x => x.Id == new MongoNewIdId(NewId.FromGuid(guidValue))).FirstAsync(); + + Assert.Equal(new MongoNewIdId(NewId.FromGuid(guidValue)), retrieved.Id); + } [Theory] [InlineData("78104553-f1cd-41ec-bcb6-d3a8ff8d994d")] @@ -357,5 +380,10 @@ public class EntityWithNullableId { public NewtonsoftJsonNewIdId? Id { get; set; } } + + public class TestDocument + { + public MongoNewIdId Id { get; set; } + } } } \ No newline at end of file diff --git a/test/StronglyTypedIds.IntegrationTests/NullableStringIdTests.cs b/test/StronglyTypedIds.IntegrationTests/NullableStringIdTests.cs index 877d3e4d1..0a23f3ece 100644 --- a/test/StronglyTypedIds.IntegrationTests/NullableStringIdTests.cs +++ b/test/StronglyTypedIds.IntegrationTests/NullableStringIdTests.cs @@ -5,6 +5,8 @@ using Dapper; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; +using MongoDB.Driver; +using StronglyTypedIds.IntegrationTests.Fixtures; using StronglyTypedIds.IntegrationTests.Types; using Xunit; using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; @@ -12,8 +14,16 @@ namespace StronglyTypedIds.IntegrationTests { + [Collection(MongoDbCollection.Name)] public class NullableStringIdTests { + private readonly MongoDbFixture _mongoDbFixture; + + public NullableStringIdTests(MongoDbFixture mongoDbFixture) + { + _mongoDbFixture = mongoDbFixture; + } + [Fact] public void SameValuesAreEqual() { @@ -201,6 +211,18 @@ public async Task WhenDapperValueConverterUsesValueConverter() var value = Assert.Single(results); Assert.Equal(value, new DapperNullableStringId("this is a value")); } + + [Fact] + public async Task WhenMongoSerializerUsesSerializer() + { + var collection = _mongoDbFixture.Database.GetCollection("MongoNullableStringIdTestCollection"); + + var original = new TestDocument { Id = new MongoNullableStringId("some value") }; + await collection.InsertOneAsync(original); + var retrieved = await collection.Find(x => x.Id == new MongoNullableStringId("some value")).FirstAsync(); + + Assert.Equal(new MongoNullableStringId("some value"), retrieved.Id); + } [Theory] [InlineData("")] @@ -407,5 +429,10 @@ public class TestEntity public Guid Id { get; set; } public EfCoreNullableStringId Name { get; set; } } + + public class TestDocument + { + public MongoNullableStringId Id { get; set; } + } } } \ No newline at end of file diff --git a/test/StronglyTypedIds.IntegrationTests/ObjectIdTests.cs b/test/StronglyTypedIds.IntegrationTests/ObjectIdTests.cs index 5b1332bc5..efc8e6184 100644 --- a/test/StronglyTypedIds.IntegrationTests/ObjectIdTests.cs +++ b/test/StronglyTypedIds.IntegrationTests/ObjectIdTests.cs @@ -6,6 +6,8 @@ using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using MongoDB.Bson; +using MongoDB.Driver; +using StronglyTypedIds.IntegrationTests.Fixtures; using StronglyTypedIds.IntegrationTests.Types; using Xunit; using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; @@ -13,8 +15,16 @@ namespace StronglyTypedIds.IntegrationTests; +[Collection(MongoDbCollection.Name)] public class ObjectIdTests { + private readonly MongoDbFixture _mongoDbFixture; + + public ObjectIdTests(MongoDbFixture mongoDbFixture) + { + _mongoDbFixture = mongoDbFixture; + } + [Fact] public void SameValuesAreEqual() { @@ -221,6 +231,19 @@ public async Task WhenDapperValueConverterUsesValueConverter() var value = Assert.Single(results); Assert.Equal(new DapperObjectIdId(new ObjectId("62758c6ee39f6ef4751bb831")), value); } + + [Fact] + public async Task WhenMongoSerializerUsesSerializer() + { + var collection = _mongoDbFixture.Database.GetCollection("ObjectIdIdTestCollection"); + + var objectIdValue = ObjectId.GenerateNewId(); + var original = new TestDocument { Id = new MongoObjectIdId(objectIdValue) }; + await collection.InsertOneAsync(original); + var retrieved = await collection.Find(x => x.Id == new MongoObjectIdId(objectIdValue)).FirstAsync(); + + Assert.Equal(new MongoObjectIdId(objectIdValue), retrieved.Id); + } [Theory] [InlineData("62758cacb6f910fe91bd697b")] @@ -357,4 +380,9 @@ public class EntityWithNullableId { public NewtonsoftJsonObjectIdId? Id { get; set; } } + + public class TestDocument + { + public MongoObjectIdId Id { get; set; } + } } \ No newline at end of file diff --git a/test/StronglyTypedIds.IntegrationTests/StringIdTests.cs b/test/StronglyTypedIds.IntegrationTests/StringIdTests.cs index 70508222c..ee92d2298 100644 --- a/test/StronglyTypedIds.IntegrationTests/StringIdTests.cs +++ b/test/StronglyTypedIds.IntegrationTests/StringIdTests.cs @@ -5,6 +5,8 @@ using Dapper; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; +using MongoDB.Driver; +using StronglyTypedIds.IntegrationTests.Fixtures; using StronglyTypedIds.IntegrationTests.Types; using Xunit; using NewtonsoftJsonSerializer = Newtonsoft.Json.JsonConvert; @@ -12,8 +14,16 @@ namespace StronglyTypedIds.IntegrationTests { + [Collection(MongoDbCollection.Name)] public class StringIdTests { + private readonly MongoDbFixture _mongoDbFixture; + + public StringIdTests(MongoDbFixture mongoDbFixture) + { + _mongoDbFixture = mongoDbFixture; + } + [Fact] public async Task ThrowsIfTryToCreateWithNull() { @@ -207,6 +217,18 @@ public async Task WhenDapperValueConverterUsesValueConverter() var value = Assert.Single(results); Assert.Equal(value, new DapperStringId("this is a value")); } + + [Fact] + public async Task WhenMongoSerializerUsesSerializer() + { + var collection = _mongoDbFixture.Database.GetCollection("StringIdTestCollection"); + + var original = new TestDocument { Id = new MongoStringId("some value") }; + await collection.InsertOneAsync(original); + var retrieved = await collection.Find(x => x.Id == new MongoStringId("some value")).FirstAsync(); + + Assert.Equal(new MongoStringId("some value"), retrieved.Id); + } [Theory] [InlineData("")] @@ -343,5 +365,10 @@ public class TestEntity public Guid Id { get; set; } public EfCoreStringId Name { get; set; } } + + public class TestDocument + { + public MongoStringId Id { get; set; } + } } } \ No newline at end of file diff --git a/test/StronglyTypedIds.IntegrationTests/StronglyTypedIds.IntegrationTests.csproj b/test/StronglyTypedIds.IntegrationTests/StronglyTypedIds.IntegrationTests.csproj index 35d734b01..1f6f3f0c1 100644 --- a/test/StronglyTypedIds.IntegrationTests/StronglyTypedIds.IntegrationTests.csproj +++ b/test/StronglyTypedIds.IntegrationTests/StronglyTypedIds.IntegrationTests.csproj @@ -19,6 +19,7 @@ + @@ -26,17 +27,21 @@ + + - + + + diff --git a/test/StronglyTypedIds.Nuget.Attributes.IntegrationTests/StronglyTypedIds.Nuget.Attributes.IntegrationTests.csproj b/test/StronglyTypedIds.Nuget.Attributes.IntegrationTests/StronglyTypedIds.Nuget.Attributes.IntegrationTests.csproj index 819f72b9c..4223049e9 100644 --- a/test/StronglyTypedIds.Nuget.Attributes.IntegrationTests/StronglyTypedIds.Nuget.Attributes.IntegrationTests.csproj +++ b/test/StronglyTypedIds.Nuget.Attributes.IntegrationTests/StronglyTypedIds.Nuget.Attributes.IntegrationTests.csproj @@ -23,23 +23,27 @@ + + + - + + diff --git a/test/StronglyTypedIds.Nuget.IntegrationTests/StronglyTypedIds.Nuget.IntegrationTests.csproj b/test/StronglyTypedIds.Nuget.IntegrationTests/StronglyTypedIds.Nuget.IntegrationTests.csproj index 1930dbe2f..0511f97f3 100644 --- a/test/StronglyTypedIds.Nuget.IntegrationTests/StronglyTypedIds.Nuget.IntegrationTests.csproj +++ b/test/StronglyTypedIds.Nuget.IntegrationTests/StronglyTypedIds.Nuget.IntegrationTests.csproj @@ -22,23 +22,27 @@ + + + - + + From 60bb3e65a7c0f9b3ff1f4fa58f47fb943d1b73ec Mon Sep 17 00:00:00 2001 From: Marcin Rogoz Date: Mon, 9 May 2022 14:36:48 +0200 Subject: [PATCH 06/12] Added missing link folder in nuget integration tests and simplified code based on review suggestion. --- src/StronglyTypedIds/Templates/NewId/NewId_MongoSerializer.cs | 4 ++-- .../StronglyTypedIds.Nuget.Attributes.IntegrationTests.csproj | 1 + .../StronglyTypedIds.Nuget.IntegrationTests.csproj | 1 + 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/StronglyTypedIds/Templates/NewId/NewId_MongoSerializer.cs b/src/StronglyTypedIds/Templates/NewId/NewId_MongoSerializer.cs index 24bf91d5e..716fa44a5 100644 --- a/src/StronglyTypedIds/Templates/NewId/NewId_MongoSerializer.cs +++ b/src/StronglyTypedIds/Templates/NewId/NewId_MongoSerializer.cs @@ -2,11 +2,11 @@ { public override TESTID Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - return new TESTID(MassTransit.NewId.FromGuid(new System.Guid(context.Reader.ReadString()))); + return new TESTID(new MassTransit.NewId(context.Reader.ReadString())); } public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, TESTID value) { - context.Writer.WriteString(value.Value.ToGuid().ToString()); + context.Writer.WriteString(value.Value.ToString()); } } \ No newline at end of file diff --git a/test/StronglyTypedIds.Nuget.Attributes.IntegrationTests/StronglyTypedIds.Nuget.Attributes.IntegrationTests.csproj b/test/StronglyTypedIds.Nuget.Attributes.IntegrationTests/StronglyTypedIds.Nuget.Attributes.IntegrationTests.csproj index 4223049e9..b99578794 100644 --- a/test/StronglyTypedIds.Nuget.Attributes.IntegrationTests/StronglyTypedIds.Nuget.Attributes.IntegrationTests.csproj +++ b/test/StronglyTypedIds.Nuget.Attributes.IntegrationTests/StronglyTypedIds.Nuget.Attributes.IntegrationTests.csproj @@ -10,6 +10,7 @@ + diff --git a/test/StronglyTypedIds.Nuget.IntegrationTests/StronglyTypedIds.Nuget.IntegrationTests.csproj b/test/StronglyTypedIds.Nuget.IntegrationTests/StronglyTypedIds.Nuget.IntegrationTests.csproj index 0511f97f3..630f9ccb8 100644 --- a/test/StronglyTypedIds.Nuget.IntegrationTests/StronglyTypedIds.Nuget.IntegrationTests.csproj +++ b/test/StronglyTypedIds.Nuget.IntegrationTests/StronglyTypedIds.Nuget.IntegrationTests.csproj @@ -9,6 +9,7 @@ + From 625a1150607f358e6c4e224ea7f424703c9d5dcc Mon Sep 17 00:00:00 2001 From: Marcin Rogoz Date: Thu, 12 May 2022 00:19:30 +0200 Subject: [PATCH 07/12] Resolved discrepancy of handling null/empty values in serializers. --- .../ObjectId/ObjectId_DapperTypeHandler.cs | 3 ++- .../ObjectId/ObjectId_EfCoreValueConverter.cs | 4 ++-- .../ObjectId/ObjectId_MongoSerializer.cs | 19 +++++++++++++++++-- .../ObjectId_NewtonsoftJsonConverter.cs | 12 +++++++++--- .../ObjectId_SystemTextJsonConverter.cs | 9 ++++++++- .../ObjectIdTests.cs | 17 ----------------- 6 files changed, 38 insertions(+), 26 deletions(-) diff --git a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_DapperTypeHandler.cs b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_DapperTypeHandler.cs index a641c2a1c..5205a903f 100644 --- a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_DapperTypeHandler.cs +++ b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_DapperTypeHandler.cs @@ -3,7 +3,7 @@ public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, TESTID value) { - parameter.Value = value.Value.ToString(); + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); } public override TESTID Parse(object value) @@ -11,6 +11,7 @@ public override TESTID Parse(object value) return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new TESTID(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => TESTID.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to TESTID"), }; } diff --git a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_EfCoreValueConverter.cs b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_EfCoreValueConverter.cs index c94a00c6c..556a3c10c 100644 --- a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_EfCoreValueConverter.cs +++ b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_EfCoreValueConverter.cs @@ -4,8 +4,8 @@ public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueC public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value.ToString(), - value => new TESTID(new MongoDB.Bson.ObjectId(value)), + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? TESTID.Empty : new TESTID(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_MongoSerializer.cs b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_MongoSerializer.cs index 63810fa83..13a403ebc 100644 --- a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_MongoSerializer.cs +++ b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_MongoSerializer.cs @@ -2,11 +2,26 @@ { public override TESTID Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - return new TESTID(context.Reader.ReadObjectId()); + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new TESTID(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return TESTID.Empty; + } } public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, TESTID value) { - context.Writer.WriteObjectId(value.Value); + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } } } \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_NewtonsoftJsonConverter.cs b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_NewtonsoftJsonConverter.cs index 332aa6322..b5e2c82de 100644 --- a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_NewtonsoftJsonConverter.cs +++ b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_NewtonsoftJsonConverter.cs @@ -8,13 +8,19 @@ public override bool CanConvert(System.Type objectType) public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - var id = (TESTID)value; - serializer.Serialize(writer, id.Value.ToString()); + if (value is TESTID id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? null : new TESTID(new MongoDB.Bson.ObjectId(result)); + return string.IsNullOrEmpty(result) ? TESTID.Empty : new TESTID(new MongoDB.Bson.ObjectId(result)); } } \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_SystemTextJsonConverter.cs b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_SystemTextJsonConverter.cs index 28c740db0..9462f2ac0 100644 --- a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_SystemTextJsonConverter.cs +++ b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_SystemTextJsonConverter.cs @@ -9,6 +9,13 @@ public override TESTID Read(ref System.Text.Json.Utf8JsonReader reader, System.T public override void Write(System.Text.Json.Utf8JsonWriter writer, TESTID value, System.Text.Json.JsonSerializerOptions options) { - writer.WriteStringValue(value.Value.ToString()); + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } } } \ No newline at end of file diff --git a/test/StronglyTypedIds.IntegrationTests/ObjectIdTests.cs b/test/StronglyTypedIds.IntegrationTests/ObjectIdTests.cs index efc8e6184..cda34b993 100644 --- a/test/StronglyTypedIds.IntegrationTests/ObjectIdTests.cs +++ b/test/StronglyTypedIds.IntegrationTests/ObjectIdTests.cs @@ -119,18 +119,6 @@ public void CanDeserializeFromObjectId_WithNewtonsoftJsonProvider() Assert.Equal(foo, deserializedFoo); } - [Fact] - public void CanSerializeToNullable_WithNewtonsoftJsonProvider() - { - var entity = new EntityWithNullableId { Id = null }; - - var json = NewtonsoftJsonSerializer.SerializeObject(entity); - var deserialize = NewtonsoftJsonSerializer.DeserializeObject(json); - - Assert.NotNull(deserialize); - Assert.Null(deserialize.Id); - } - [Fact] public void CanDeserializeFromObjectId_WithSystemTextJsonProvider() { @@ -376,11 +364,6 @@ public class TestEntity public EfCoreObjectIdId Id { get; set; } } - public class EntityWithNullableId - { - public NewtonsoftJsonObjectIdId? Id { get; set; } - } - public class TestDocument { public MongoObjectIdId Id { get; set; } From de0c801a6c9d6841686113676615bd850e420baf Mon Sep 17 00:00:00 2001 From: Marcin Rogoz Date: Sun, 15 May 2022 20:59:05 +0200 Subject: [PATCH 08/12] Reverted CanSerializeToNullable test. --- .../ObjectIdTests.cs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/test/StronglyTypedIds.IntegrationTests/ObjectIdTests.cs b/test/StronglyTypedIds.IntegrationTests/ObjectIdTests.cs index cda34b993..faa471c03 100644 --- a/test/StronglyTypedIds.IntegrationTests/ObjectIdTests.cs +++ b/test/StronglyTypedIds.IntegrationTests/ObjectIdTests.cs @@ -95,6 +95,18 @@ public void CanSerializeToObjectId_WithTypeConverter() Assert.Equal(serializedFoo, serializedObjectId); } + + [Fact] + public void CanSerializeToNullable_WithNewtonsoftJsonProvider() + { + var entity = new EntityWithNullableId { Id = null }; + + var json = NewtonsoftJsonSerializer.SerializeObject(entity); + var deserialize = NewtonsoftJsonSerializer.DeserializeObject(json); + + Assert.NotNull(deserialize); + Assert.Equal(deserialize.Id, NewtonsoftJsonObjectIdId.Empty); + } [Fact] public void CanSerializeToObjectId_WithSystemTextJsonProvider() @@ -363,6 +375,11 @@ public class TestEntity { public EfCoreObjectIdId Id { get; set; } } + + public class EntityWithNullableId + { + public NewtonsoftJsonObjectIdId? Id { get; set; } + } public class TestDocument { From f2d63d1b062def078496e49b538c319a28b8276d Mon Sep 17 00:00:00 2001 From: Marcin Rogoz Date: Sun, 15 May 2022 21:38:51 +0200 Subject: [PATCH 09/12] Added missing empty line to ObjectId_Base template and generated snaphot files. --- .../Templates/ObjectId/ObjectId_Base.cs | 3 +- ...ce=StronglyTypedIdBackingType.verified.txt | 1 + ...urce=StronglyTypedIdConverter.verified.txt | 5 + ...pe=ObjectId_implementations=0.verified.txt | 39 ++++ ...pe=ObjectId_implementations=2.verified.txt | 39 ++++ ...pe=ObjectId_implementations=4.verified.txt | 40 ++++ ...pe=ObjectId_implementations=6.verified.txt | 40 ++++ ...ngType=Guid_implementations=0.verified.txt | 91 ++++++++ ...ngType=Guid_implementations=2.verified.txt | 91 ++++++++ ...ngType=Guid_implementations=4.verified.txt | 92 ++++++++ ...ngType=Guid_implementations=6.verified.txt | 92 ++++++++ ...ingType=Int_implementations=0.verified.txt | 91 ++++++++ ...ingType=Int_implementations=2.verified.txt | 91 ++++++++ ...ingType=Int_implementations=4.verified.txt | 92 ++++++++ ...ingType=Int_implementations=6.verified.txt | 92 ++++++++ ...ngType=Long_implementations=0.verified.txt | 92 ++++++++ ...ngType=Long_implementations=2.verified.txt | 92 ++++++++ ...ngType=Long_implementations=4.verified.txt | 93 ++++++++ ...ngType=Long_implementations=6.verified.txt | 93 ++++++++ ...ransitNewId_implementations=0.verified.txt | 91 ++++++++ ...ransitNewId_implementations=2.verified.txt | 91 ++++++++ ...ransitNewId_implementations=4.verified.txt | 92 ++++++++ ...ransitNewId_implementations=6.verified.txt | 92 ++++++++ ...lableString_implementations=0.verified.txt | 113 ++++++++++ ...lableString_implementations=2.verified.txt | 113 ++++++++++ ...lableString_implementations=4.verified.txt | 123 +++++++++++ ...lableString_implementations=6.verified.txt | 123 +++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 112 ++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 112 ++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 113 ++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 113 ++++++++++ ...Type=String_implementations=0.verified.txt | 97 +++++++++ ...Type=String_implementations=2.verified.txt | 97 +++++++++ ...Type=String_implementations=4.verified.txt | 107 ++++++++++ ...Type=String_implementations=6.verified.txt | 107 ++++++++++ ...ngType=Guid_implementations=0.verified.txt | 133 ++++++++++++ ...ngType=Guid_implementations=2.verified.txt | 133 ++++++++++++ ...ngType=Guid_implementations=4.verified.txt | 134 ++++++++++++ ...ngType=Guid_implementations=6.verified.txt | 134 ++++++++++++ ...ingType=Int_implementations=0.verified.txt | 133 ++++++++++++ ...ingType=Int_implementations=2.verified.txt | 133 ++++++++++++ ...ingType=Int_implementations=4.verified.txt | 134 ++++++++++++ ...ingType=Int_implementations=6.verified.txt | 134 ++++++++++++ ...ngType=Long_implementations=0.verified.txt | 136 ++++++++++++ ...ngType=Long_implementations=2.verified.txt | 136 ++++++++++++ ...ngType=Long_implementations=4.verified.txt | 137 ++++++++++++ ...ngType=Long_implementations=6.verified.txt | 137 ++++++++++++ ...ransitNewId_implementations=0.verified.txt | 141 +++++++++++++ ...ransitNewId_implementations=2.verified.txt | 141 +++++++++++++ ...ransitNewId_implementations=4.verified.txt | 142 +++++++++++++ ...ransitNewId_implementations=6.verified.txt | 142 +++++++++++++ ...lableString_implementations=0.verified.txt | 156 ++++++++++++++ ...lableString_implementations=2.verified.txt | 156 ++++++++++++++ ...lableString_implementations=4.verified.txt | 166 +++++++++++++++ ...lableString_implementations=6.verified.txt | 166 +++++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 155 ++++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 155 ++++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 156 ++++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 156 ++++++++++++++ ...Type=String_implementations=0.verified.txt | 135 ++++++++++++ ...Type=String_implementations=2.verified.txt | 135 ++++++++++++ ...Type=String_implementations=4.verified.txt | 145 +++++++++++++ ...Type=String_implementations=6.verified.txt | 145 +++++++++++++ ...ngType=Guid_implementations=0.verified.txt | 84 ++++++++ ...ngType=Guid_implementations=2.verified.txt | 84 ++++++++ ...ngType=Guid_implementations=4.verified.txt | 85 ++++++++ ...ngType=Guid_implementations=6.verified.txt | 85 ++++++++ ...ingType=Int_implementations=0.verified.txt | 84 ++++++++ ...ingType=Int_implementations=2.verified.txt | 84 ++++++++ ...ingType=Int_implementations=4.verified.txt | 85 ++++++++ ...ingType=Int_implementations=6.verified.txt | 85 ++++++++ ...ngType=Long_implementations=0.verified.txt | 85 ++++++++ ...ngType=Long_implementations=2.verified.txt | 85 ++++++++ ...ngType=Long_implementations=4.verified.txt | 86 ++++++++ ...ngType=Long_implementations=6.verified.txt | 86 ++++++++ ...ransitNewId_implementations=0.verified.txt | 84 ++++++++ ...ransitNewId_implementations=2.verified.txt | 84 ++++++++ ...ransitNewId_implementations=4.verified.txt | 85 ++++++++ ...ransitNewId_implementations=6.verified.txt | 85 ++++++++ ...lableString_implementations=0.verified.txt | 107 ++++++++++ ...lableString_implementations=2.verified.txt | 107 ++++++++++ ...lableString_implementations=4.verified.txt | 117 +++++++++++ ...lableString_implementations=6.verified.txt | 117 +++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 107 ++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 107 ++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 108 ++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 108 ++++++++++ ...Type=String_implementations=0.verified.txt | 91 ++++++++ ...Type=String_implementations=2.verified.txt | 91 ++++++++ ...Type=String_implementations=4.verified.txt | 101 +++++++++ ...Type=String_implementations=6.verified.txt | 101 +++++++++ ...ngType=Guid_implementations=0.verified.txt | 126 +++++++++++ ...ngType=Guid_implementations=2.verified.txt | 126 +++++++++++ ...ngType=Guid_implementations=4.verified.txt | 127 +++++++++++ ...ngType=Guid_implementations=6.verified.txt | 127 +++++++++++ ...ingType=Int_implementations=0.verified.txt | 126 +++++++++++ ...ingType=Int_implementations=2.verified.txt | 126 +++++++++++ ...ingType=Int_implementations=4.verified.txt | 127 +++++++++++ ...ingType=Int_implementations=6.verified.txt | 127 +++++++++++ ...ngType=Long_implementations=0.verified.txt | 129 ++++++++++++ ...ngType=Long_implementations=2.verified.txt | 129 ++++++++++++ ...ngType=Long_implementations=4.verified.txt | 130 ++++++++++++ ...ngType=Long_implementations=6.verified.txt | 130 ++++++++++++ ...ransitNewId_implementations=0.verified.txt | 134 ++++++++++++ ...ransitNewId_implementations=2.verified.txt | 134 ++++++++++++ ...ransitNewId_implementations=4.verified.txt | 135 ++++++++++++ ...ransitNewId_implementations=6.verified.txt | 135 ++++++++++++ ...lableString_implementations=0.verified.txt | 150 +++++++++++++ ...lableString_implementations=2.verified.txt | 150 +++++++++++++ ...lableString_implementations=4.verified.txt | 160 ++++++++++++++ ...lableString_implementations=6.verified.txt | 160 ++++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 150 +++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 150 +++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 151 +++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 151 +++++++++++++ ...Type=String_implementations=0.verified.txt | 129 ++++++++++++ ...Type=String_implementations=2.verified.txt | 129 ++++++++++++ ...Type=String_implementations=4.verified.txt | 139 ++++++++++++ ...Type=String_implementations=6.verified.txt | 139 ++++++++++++ ...ngType=Guid_implementations=0.verified.txt | 105 ++++++++++ ...ngType=Guid_implementations=2.verified.txt | 105 ++++++++++ ...ngType=Guid_implementations=4.verified.txt | 106 ++++++++++ ...ngType=Guid_implementations=6.verified.txt | 106 ++++++++++ ...ingType=Int_implementations=0.verified.txt | 105 ++++++++++ ...ingType=Int_implementations=2.verified.txt | 105 ++++++++++ ...ingType=Int_implementations=4.verified.txt | 106 ++++++++++ ...ingType=Int_implementations=6.verified.txt | 106 ++++++++++ ...ngType=Long_implementations=0.verified.txt | 106 ++++++++++ ...ngType=Long_implementations=2.verified.txt | 106 ++++++++++ ...ngType=Long_implementations=4.verified.txt | 107 ++++++++++ ...ngType=Long_implementations=6.verified.txt | 107 ++++++++++ ...ransitNewId_implementations=0.verified.txt | 105 ++++++++++ ...ransitNewId_implementations=2.verified.txt | 105 ++++++++++ ...ransitNewId_implementations=4.verified.txt | 106 ++++++++++ ...ransitNewId_implementations=6.verified.txt | 106 ++++++++++ ...lableString_implementations=0.verified.txt | 134 ++++++++++++ ...lableString_implementations=2.verified.txt | 134 ++++++++++++ ...lableString_implementations=4.verified.txt | 144 +++++++++++++ ...lableString_implementations=6.verified.txt | 144 +++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 134 ++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 134 ++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 135 ++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 135 ++++++++++++ ...Type=String_implementations=0.verified.txt | 111 ++++++++++ ...Type=String_implementations=2.verified.txt | 111 ++++++++++ ...Type=String_implementations=4.verified.txt | 121 +++++++++++ ...Type=String_implementations=6.verified.txt | 121 +++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 104 +++++++++ ...pe=ObjectId_implementations=2.verified.txt | 104 +++++++++ ...pe=ObjectId_implementations=4.verified.txt | 105 ++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 105 ++++++++++ ...ngType=Guid_implementations=0.verified.txt | 147 +++++++++++++ ...ngType=Guid_implementations=2.verified.txt | 147 +++++++++++++ ...ngType=Guid_implementations=4.verified.txt | 148 +++++++++++++ ...ngType=Guid_implementations=6.verified.txt | 148 +++++++++++++ ...ingType=Int_implementations=0.verified.txt | 147 +++++++++++++ ...ingType=Int_implementations=2.verified.txt | 147 +++++++++++++ ...ingType=Int_implementations=4.verified.txt | 148 +++++++++++++ ...ingType=Int_implementations=6.verified.txt | 148 +++++++++++++ ...ngType=Long_implementations=0.verified.txt | 150 +++++++++++++ ...ngType=Long_implementations=2.verified.txt | 150 +++++++++++++ ...ngType=Long_implementations=4.verified.txt | 151 +++++++++++++ ...ngType=Long_implementations=6.verified.txt | 151 +++++++++++++ ...ransitNewId_implementations=0.verified.txt | 155 ++++++++++++++ ...ransitNewId_implementations=2.verified.txt | 155 ++++++++++++++ ...ransitNewId_implementations=4.verified.txt | 156 ++++++++++++++ ...ransitNewId_implementations=6.verified.txt | 156 ++++++++++++++ ...lableString_implementations=0.verified.txt | 177 ++++++++++++++++ ...lableString_implementations=2.verified.txt | 177 ++++++++++++++++ ...lableString_implementations=4.verified.txt | 187 +++++++++++++++++ ...lableString_implementations=6.verified.txt | 187 +++++++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 177 ++++++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 177 ++++++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 178 ++++++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 178 ++++++++++++++++ ...Type=String_implementations=0.verified.txt | 149 +++++++++++++ ...Type=String_implementations=2.verified.txt | 149 +++++++++++++ ...Type=String_implementations=4.verified.txt | 159 ++++++++++++++ ...Type=String_implementations=6.verified.txt | 159 ++++++++++++++ ...ngType=Guid_implementations=0.verified.txt | 81 +++++++ ...ngType=Guid_implementations=2.verified.txt | 81 +++++++ ...ngType=Guid_implementations=4.verified.txt | 82 ++++++++ ...ngType=Guid_implementations=6.verified.txt | 82 ++++++++ ...ingType=Int_implementations=0.verified.txt | 81 +++++++ ...ingType=Int_implementations=2.verified.txt | 81 +++++++ ...ingType=Int_implementations=4.verified.txt | 82 ++++++++ ...ingType=Int_implementations=6.verified.txt | 82 ++++++++ ...ngType=Long_implementations=0.verified.txt | 82 ++++++++ ...ngType=Long_implementations=2.verified.txt | 82 ++++++++ ...ngType=Long_implementations=4.verified.txt | 83 ++++++++ ...ngType=Long_implementations=6.verified.txt | 83 ++++++++ ...ransitNewId_implementations=0.verified.txt | 81 +++++++ ...ransitNewId_implementations=2.verified.txt | 81 +++++++ ...ransitNewId_implementations=4.verified.txt | 82 ++++++++ ...ransitNewId_implementations=6.verified.txt | 82 ++++++++ ...lableString_implementations=0.verified.txt | 97 +++++++++ ...lableString_implementations=2.verified.txt | 97 +++++++++ ...lableString_implementations=4.verified.txt | 107 ++++++++++ ...lableString_implementations=6.verified.txt | 107 ++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 96 +++++++++ ...pe=ObjectId_implementations=2.verified.txt | 96 +++++++++ ...pe=ObjectId_implementations=4.verified.txt | 97 +++++++++ ...pe=ObjectId_implementations=6.verified.txt | 97 +++++++++ ...Type=String_implementations=0.verified.txt | 88 ++++++++ ...Type=String_implementations=2.verified.txt | 88 ++++++++ ...Type=String_implementations=4.verified.txt | 98 +++++++++ ...Type=String_implementations=6.verified.txt | 98 +++++++++ ...ngType=Guid_implementations=0.verified.txt | 123 +++++++++++ ...ngType=Guid_implementations=2.verified.txt | 123 +++++++++++ ...ngType=Guid_implementations=4.verified.txt | 124 +++++++++++ ...ngType=Guid_implementations=6.verified.txt | 124 +++++++++++ ...ingType=Int_implementations=0.verified.txt | 123 +++++++++++ ...ingType=Int_implementations=2.verified.txt | 123 +++++++++++ ...ingType=Int_implementations=4.verified.txt | 124 +++++++++++ ...ingType=Int_implementations=6.verified.txt | 124 +++++++++++ ...ngType=Long_implementations=0.verified.txt | 126 +++++++++++ ...ngType=Long_implementations=2.verified.txt | 126 +++++++++++ ...ngType=Long_implementations=4.verified.txt | 127 +++++++++++ ...ngType=Long_implementations=6.verified.txt | 127 +++++++++++ ...ransitNewId_implementations=0.verified.txt | 131 ++++++++++++ ...ransitNewId_implementations=2.verified.txt | 131 ++++++++++++ ...ransitNewId_implementations=4.verified.txt | 132 ++++++++++++ ...ransitNewId_implementations=6.verified.txt | 132 ++++++++++++ ...lableString_implementations=0.verified.txt | 140 +++++++++++++ ...lableString_implementations=2.verified.txt | 140 +++++++++++++ ...lableString_implementations=4.verified.txt | 150 +++++++++++++ ...lableString_implementations=6.verified.txt | 150 +++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 139 ++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 139 ++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 140 +++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 140 +++++++++++++ ...Type=String_implementations=0.verified.txt | 126 +++++++++++ ...Type=String_implementations=2.verified.txt | 126 +++++++++++ ...Type=String_implementations=4.verified.txt | 136 ++++++++++++ ...Type=String_implementations=6.verified.txt | 136 ++++++++++++ ...ngType=Guid_implementations=0.verified.txt | 102 +++++++++ ...ngType=Guid_implementations=2.verified.txt | 102 +++++++++ ...ngType=Guid_implementations=4.verified.txt | 103 +++++++++ ...ngType=Guid_implementations=6.verified.txt | 103 +++++++++ ...ingType=Int_implementations=0.verified.txt | 102 +++++++++ ...ingType=Int_implementations=2.verified.txt | 102 +++++++++ ...ingType=Int_implementations=4.verified.txt | 103 +++++++++ ...ingType=Int_implementations=6.verified.txt | 103 +++++++++ ...ngType=Long_implementations=0.verified.txt | 103 +++++++++ ...ngType=Long_implementations=2.verified.txt | 103 +++++++++ ...ngType=Long_implementations=4.verified.txt | 104 +++++++++ ...ngType=Long_implementations=6.verified.txt | 104 +++++++++ ...ransitNewId_implementations=0.verified.txt | 102 +++++++++ ...ransitNewId_implementations=2.verified.txt | 102 +++++++++ ...ransitNewId_implementations=4.verified.txt | 103 +++++++++ ...ransitNewId_implementations=6.verified.txt | 103 +++++++++ ...lableString_implementations=0.verified.txt | 124 +++++++++++ ...lableString_implementations=2.verified.txt | 124 +++++++++++ ...lableString_implementations=4.verified.txt | 134 ++++++++++++ ...lableString_implementations=6.verified.txt | 134 ++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 123 +++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 123 +++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 124 +++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 124 +++++++++++ ...Type=String_implementations=0.verified.txt | 108 ++++++++++ ...Type=String_implementations=2.verified.txt | 108 ++++++++++ ...Type=String_implementations=4.verified.txt | 118 +++++++++++ ...Type=String_implementations=6.verified.txt | 118 +++++++++++ ...ngType=Guid_implementations=0.verified.txt | 144 +++++++++++++ ...ngType=Guid_implementations=2.verified.txt | 144 +++++++++++++ ...ngType=Guid_implementations=4.verified.txt | 145 +++++++++++++ ...ngType=Guid_implementations=6.verified.txt | 145 +++++++++++++ ...ingType=Int_implementations=0.verified.txt | 144 +++++++++++++ ...ingType=Int_implementations=2.verified.txt | 144 +++++++++++++ ...ingType=Int_implementations=4.verified.txt | 145 +++++++++++++ ...ingType=Int_implementations=6.verified.txt | 145 +++++++++++++ ...ngType=Long_implementations=0.verified.txt | 147 +++++++++++++ ...ngType=Long_implementations=2.verified.txt | 147 +++++++++++++ ...ngType=Long_implementations=4.verified.txt | 148 +++++++++++++ ...ngType=Long_implementations=6.verified.txt | 148 +++++++++++++ ...ransitNewId_implementations=0.verified.txt | 152 ++++++++++++++ ...ransitNewId_implementations=2.verified.txt | 152 ++++++++++++++ ...ransitNewId_implementations=4.verified.txt | 153 ++++++++++++++ ...ransitNewId_implementations=6.verified.txt | 153 ++++++++++++++ ...lableString_implementations=0.verified.txt | 167 +++++++++++++++ ...lableString_implementations=2.verified.txt | 167 +++++++++++++++ ...lableString_implementations=4.verified.txt | 177 ++++++++++++++++ ...lableString_implementations=6.verified.txt | 177 ++++++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 166 +++++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 166 +++++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 167 +++++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 167 +++++++++++++++ ...Type=String_implementations=0.verified.txt | 146 +++++++++++++ ...Type=String_implementations=2.verified.txt | 146 +++++++++++++ ...Type=String_implementations=4.verified.txt | 156 ++++++++++++++ ...Type=String_implementations=6.verified.txt | 156 ++++++++++++++ ...ngType=Guid_implementations=0.verified.txt | 95 +++++++++ ...ngType=Guid_implementations=2.verified.txt | 95 +++++++++ ...ngType=Guid_implementations=4.verified.txt | 96 +++++++++ ...ngType=Guid_implementations=6.verified.txt | 96 +++++++++ ...ingType=Int_implementations=0.verified.txt | 95 +++++++++ ...ingType=Int_implementations=2.verified.txt | 95 +++++++++ ...ingType=Int_implementations=4.verified.txt | 96 +++++++++ ...ingType=Int_implementations=6.verified.txt | 96 +++++++++ ...ngType=Long_implementations=0.verified.txt | 96 +++++++++ ...ngType=Long_implementations=2.verified.txt | 96 +++++++++ ...ngType=Long_implementations=4.verified.txt | 97 +++++++++ ...ngType=Long_implementations=6.verified.txt | 97 +++++++++ ...ransitNewId_implementations=0.verified.txt | 95 +++++++++ ...ransitNewId_implementations=2.verified.txt | 95 +++++++++ ...ransitNewId_implementations=4.verified.txt | 96 +++++++++ ...ransitNewId_implementations=6.verified.txt | 96 +++++++++ ...lableString_implementations=0.verified.txt | 118 +++++++++++ ...lableString_implementations=2.verified.txt | 118 +++++++++++ ...lableString_implementations=4.verified.txt | 128 +++++++++++ ...lableString_implementations=6.verified.txt | 128 +++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 118 +++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 118 +++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 119 +++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 119 +++++++++++ ...Type=String_implementations=0.verified.txt | 102 +++++++++ ...Type=String_implementations=2.verified.txt | 102 +++++++++ ...Type=String_implementations=4.verified.txt | 112 ++++++++++ ...Type=String_implementations=6.verified.txt | 112 ++++++++++ ...ngType=Guid_implementations=0.verified.txt | 137 ++++++++++++ ...ngType=Guid_implementations=2.verified.txt | 137 ++++++++++++ ...ngType=Guid_implementations=4.verified.txt | 138 ++++++++++++ ...ngType=Guid_implementations=6.verified.txt | 138 ++++++++++++ ...ingType=Int_implementations=0.verified.txt | 137 ++++++++++++ ...ingType=Int_implementations=2.verified.txt | 137 ++++++++++++ ...ingType=Int_implementations=4.verified.txt | 138 ++++++++++++ ...ingType=Int_implementations=6.verified.txt | 138 ++++++++++++ ...ngType=Long_implementations=0.verified.txt | 140 +++++++++++++ ...ngType=Long_implementations=2.verified.txt | 140 +++++++++++++ ...ngType=Long_implementations=4.verified.txt | 141 +++++++++++++ ...ngType=Long_implementations=6.verified.txt | 141 +++++++++++++ ...ransitNewId_implementations=0.verified.txt | 145 +++++++++++++ ...ransitNewId_implementations=2.verified.txt | 145 +++++++++++++ ...ransitNewId_implementations=4.verified.txt | 146 +++++++++++++ ...ransitNewId_implementations=6.verified.txt | 146 +++++++++++++ ...lableString_implementations=0.verified.txt | 161 ++++++++++++++ ...lableString_implementations=2.verified.txt | 161 ++++++++++++++ ...lableString_implementations=4.verified.txt | 171 +++++++++++++++ ...lableString_implementations=6.verified.txt | 171 +++++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 161 ++++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 161 ++++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 162 ++++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 162 ++++++++++++++ ...Type=String_implementations=0.verified.txt | 140 +++++++++++++ ...Type=String_implementations=2.verified.txt | 140 +++++++++++++ ...Type=String_implementations=4.verified.txt | 150 +++++++++++++ ...Type=String_implementations=6.verified.txt | 150 +++++++++++++ ...ngType=Guid_implementations=0.verified.txt | 116 ++++++++++ ...ngType=Guid_implementations=2.verified.txt | 116 ++++++++++ ...ngType=Guid_implementations=4.verified.txt | 117 +++++++++++ ...ngType=Guid_implementations=6.verified.txt | 117 +++++++++++ ...ingType=Int_implementations=0.verified.txt | 116 ++++++++++ ...ingType=Int_implementations=2.verified.txt | 116 ++++++++++ ...ingType=Int_implementations=4.verified.txt | 117 +++++++++++ ...ingType=Int_implementations=6.verified.txt | 117 +++++++++++ ...ngType=Long_implementations=0.verified.txt | 117 +++++++++++ ...ngType=Long_implementations=2.verified.txt | 117 +++++++++++ ...ngType=Long_implementations=4.verified.txt | 118 +++++++++++ ...ngType=Long_implementations=6.verified.txt | 118 +++++++++++ ...ransitNewId_implementations=0.verified.txt | 116 ++++++++++ ...ransitNewId_implementations=2.verified.txt | 116 ++++++++++ ...ransitNewId_implementations=4.verified.txt | 117 +++++++++++ ...ransitNewId_implementations=6.verified.txt | 117 +++++++++++ ...lableString_implementations=0.verified.txt | 145 +++++++++++++ ...lableString_implementations=2.verified.txt | 145 +++++++++++++ ...lableString_implementations=4.verified.txt | 155 ++++++++++++++ ...lableString_implementations=6.verified.txt | 155 ++++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 145 +++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 145 +++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 146 +++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 146 +++++++++++++ ...Type=String_implementations=0.verified.txt | 122 +++++++++++ ...Type=String_implementations=2.verified.txt | 122 +++++++++++ ...Type=String_implementations=4.verified.txt | 132 ++++++++++++ ...Type=String_implementations=6.verified.txt | 132 ++++++++++++ ...ngType=Guid_implementations=0.verified.txt | 158 ++++++++++++++ ...ngType=Guid_implementations=2.verified.txt | 158 ++++++++++++++ ...ngType=Guid_implementations=4.verified.txt | 159 ++++++++++++++ ...ngType=Guid_implementations=6.verified.txt | 159 ++++++++++++++ ...ingType=Int_implementations=0.verified.txt | 158 ++++++++++++++ ...ingType=Int_implementations=2.verified.txt | 158 ++++++++++++++ ...ingType=Int_implementations=4.verified.txt | 159 ++++++++++++++ ...ingType=Int_implementations=6.verified.txt | 159 ++++++++++++++ ...ngType=Long_implementations=0.verified.txt | 161 ++++++++++++++ ...ngType=Long_implementations=2.verified.txt | 161 ++++++++++++++ ...ngType=Long_implementations=4.verified.txt | 162 ++++++++++++++ ...ngType=Long_implementations=6.verified.txt | 162 ++++++++++++++ ...ransitNewId_implementations=0.verified.txt | 166 +++++++++++++++ ...ransitNewId_implementations=2.verified.txt | 166 +++++++++++++++ ...ransitNewId_implementations=4.verified.txt | 167 +++++++++++++++ ...ransitNewId_implementations=6.verified.txt | 167 +++++++++++++++ ...lableString_implementations=0.verified.txt | 188 +++++++++++++++++ ...lableString_implementations=2.verified.txt | 188 +++++++++++++++++ ...lableString_implementations=4.verified.txt | 198 ++++++++++++++++++ ...lableString_implementations=6.verified.txt | 198 ++++++++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 188 +++++++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 188 +++++++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 189 +++++++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 189 +++++++++++++++++ ...Type=String_implementations=0.verified.txt | 160 ++++++++++++++ ...Type=String_implementations=2.verified.txt | 160 ++++++++++++++ ...Type=String_implementations=4.verified.txt | 170 +++++++++++++++ ...Type=String_implementations=6.verified.txt | 170 +++++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 88 ++++++++ ...pe=ObjectId_implementations=2.verified.txt | 88 ++++++++ ...pe=ObjectId_implementations=4.verified.txt | 89 ++++++++ ...pe=ObjectId_implementations=6.verified.txt | 89 ++++++++ ...pe=ObjectId_implementations=0.verified.txt | 131 ++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 131 ++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 132 ++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 132 ++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 50 +++++ ...pe=ObjectId_implementations=2.verified.txt | 50 +++++ ...pe=ObjectId_implementations=4.verified.txt | 51 +++++ ...pe=ObjectId_implementations=6.verified.txt | 51 +++++ ...pe=ObjectId_implementations=0.verified.txt | 93 ++++++++ ...pe=ObjectId_implementations=2.verified.txt | 93 ++++++++ ...pe=ObjectId_implementations=4.verified.txt | 94 +++++++++ ...pe=ObjectId_implementations=6.verified.txt | 94 +++++++++ ...pe=ObjectId_implementations=0.verified.txt | 77 +++++++ ...pe=ObjectId_implementations=2.verified.txt | 77 +++++++ ...pe=ObjectId_implementations=4.verified.txt | 78 +++++++ ...pe=ObjectId_implementations=6.verified.txt | 78 +++++++ ...pe=ObjectId_implementations=0.verified.txt | 120 +++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 120 +++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 121 +++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 121 +++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 72 +++++++ ...pe=ObjectId_implementations=2.verified.txt | 72 +++++++ ...pe=ObjectId_implementations=4.verified.txt | 73 +++++++ ...pe=ObjectId_implementations=6.verified.txt | 73 +++++++ ...pe=ObjectId_implementations=0.verified.txt | 115 ++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 115 ++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 116 ++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 116 ++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 99 +++++++++ ...pe=ObjectId_implementations=2.verified.txt | 99 +++++++++ ...pe=ObjectId_implementations=4.verified.txt | 100 +++++++++ ...pe=ObjectId_implementations=6.verified.txt | 100 +++++++++ ...pe=ObjectId_implementations=0.verified.txt | 82 ++++++++ ...pe=ObjectId_implementations=2.verified.txt | 82 ++++++++ ...pe=ObjectId_implementations=4.verified.txt | 83 ++++++++ ...pe=ObjectId_implementations=6.verified.txt | 83 ++++++++ ...pe=ObjectId_implementations=0.verified.txt | 142 +++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 142 +++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 143 +++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 143 +++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 57 +++++ ...pe=ObjectId_implementations=2.verified.txt | 57 +++++ ...pe=ObjectId_implementations=4.verified.txt | 58 +++++ ...pe=ObjectId_implementations=6.verified.txt | 58 +++++ ...pe=ObjectId_implementations=0.verified.txt | 100 +++++++++ ...pe=ObjectId_implementations=2.verified.txt | 100 +++++++++ ...pe=ObjectId_implementations=4.verified.txt | 101 +++++++++ ...pe=ObjectId_implementations=6.verified.txt | 101 +++++++++ ...pe=ObjectId_implementations=0.verified.txt | 84 ++++++++ ...pe=ObjectId_implementations=2.verified.txt | 84 ++++++++ ...pe=ObjectId_implementations=4.verified.txt | 85 ++++++++ ...pe=ObjectId_implementations=6.verified.txt | 85 ++++++++ ...pe=ObjectId_implementations=0.verified.txt | 127 +++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 127 +++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 128 +++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 128 +++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 79 +++++++ ...pe=ObjectId_implementations=2.verified.txt | 79 +++++++ ...pe=ObjectId_implementations=4.verified.txt | 80 +++++++ ...pe=ObjectId_implementations=6.verified.txt | 80 +++++++ ...pe=ObjectId_implementations=0.verified.txt | 122 +++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 122 +++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 123 +++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 123 +++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 106 ++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 106 ++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 107 ++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 107 ++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 149 +++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 149 +++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 150 +++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 150 +++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 68 ++++++ ...pe=ObjectId_implementations=2.verified.txt | 68 ++++++ ...pe=ObjectId_implementations=4.verified.txt | 69 ++++++ ...pe=ObjectId_implementations=6.verified.txt | 69 ++++++ ...pe=ObjectId_implementations=0.verified.txt | 66 ++++++ ...pe=ObjectId_implementations=2.verified.txt | 66 ++++++ ...pe=ObjectId_implementations=4.verified.txt | 67 ++++++ ...pe=ObjectId_implementations=6.verified.txt | 67 ++++++ ...pe=ObjectId_implementations=0.verified.txt | 111 ++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 111 ++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 112 ++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 112 ++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 95 +++++++++ ...pe=ObjectId_implementations=2.verified.txt | 95 +++++++++ ...pe=ObjectId_implementations=4.verified.txt | 96 +++++++++ ...pe=ObjectId_implementations=6.verified.txt | 96 +++++++++ ...pe=ObjectId_implementations=0.verified.txt | 138 ++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 138 ++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 139 ++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 139 ++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 90 ++++++++ ...pe=ObjectId_implementations=2.verified.txt | 90 ++++++++ ...pe=ObjectId_implementations=4.verified.txt | 91 ++++++++ ...pe=ObjectId_implementations=6.verified.txt | 91 ++++++++ ...pe=ObjectId_implementations=0.verified.txt | 133 ++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 133 ++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 134 ++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 134 ++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 117 +++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 117 +++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 118 +++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 118 +++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 160 ++++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 160 ++++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 161 ++++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 161 ++++++++++++++ ...ngType=Guid_implementations=0.verified.txt | 52 +++++ ...ngType=Guid_implementations=2.verified.txt | 52 +++++ ...ngType=Guid_implementations=4.verified.txt | 53 +++++ ...ngType=Guid_implementations=6.verified.txt | 53 +++++ ...ingType=Int_implementations=0.verified.txt | 51 +++++ ...ingType=Int_implementations=2.verified.txt | 51 +++++ ...ingType=Int_implementations=4.verified.txt | 52 +++++ ...ingType=Int_implementations=6.verified.txt | 52 +++++ ...ngType=Long_implementations=0.verified.txt | 51 +++++ ...ngType=Long_implementations=2.verified.txt | 51 +++++ ...ngType=Long_implementations=4.verified.txt | 52 +++++ ...ngType=Long_implementations=6.verified.txt | 52 +++++ ...ransitNewId_implementations=0.verified.txt | 52 +++++ ...ransitNewId_implementations=2.verified.txt | 52 +++++ ...ransitNewId_implementations=4.verified.txt | 53 +++++ ...ransitNewId_implementations=6.verified.txt | 53 +++++ ...lableString_implementations=0.verified.txt | 67 ++++++ ...lableString_implementations=2.verified.txt | 67 ++++++ ...lableString_implementations=4.verified.txt | 77 +++++++ ...lableString_implementations=6.verified.txt | 77 +++++++ ...pe=ObjectId_implementations=0.verified.txt | 67 ++++++ ...pe=ObjectId_implementations=2.verified.txt | 67 ++++++ ...pe=ObjectId_implementations=4.verified.txt | 68 ++++++ ...pe=ObjectId_implementations=6.verified.txt | 68 ++++++ ...Type=String_implementations=0.verified.txt | 60 ++++++ ...Type=String_implementations=2.verified.txt | 60 ++++++ ...Type=String_implementations=4.verified.txt | 70 +++++++ ...Type=String_implementations=6.verified.txt | 70 +++++++ ...ngType=Guid_implementations=0.verified.txt | 94 +++++++++ ...ngType=Guid_implementations=2.verified.txt | 94 +++++++++ ...ngType=Guid_implementations=4.verified.txt | 95 +++++++++ ...ngType=Guid_implementations=6.verified.txt | 95 +++++++++ ...ingType=Int_implementations=0.verified.txt | 93 ++++++++ ...ingType=Int_implementations=2.verified.txt | 93 ++++++++ ...ingType=Int_implementations=4.verified.txt | 94 +++++++++ ...ingType=Int_implementations=6.verified.txt | 94 +++++++++ ...ngType=Long_implementations=0.verified.txt | 95 +++++++++ ...ngType=Long_implementations=2.verified.txt | 95 +++++++++ ...ngType=Long_implementations=4.verified.txt | 96 +++++++++ ...ngType=Long_implementations=6.verified.txt | 96 +++++++++ ...ransitNewId_implementations=0.verified.txt | 102 +++++++++ ...ransitNewId_implementations=2.verified.txt | 102 +++++++++ ...ransitNewId_implementations=4.verified.txt | 103 +++++++++ ...ransitNewId_implementations=6.verified.txt | 103 +++++++++ ...lableString_implementations=0.verified.txt | 110 ++++++++++ ...lableString_implementations=2.verified.txt | 110 ++++++++++ ...lableString_implementations=4.verified.txt | 120 +++++++++++ ...lableString_implementations=6.verified.txt | 120 +++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 110 ++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 110 ++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 111 ++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 111 ++++++++++ ...Type=String_implementations=0.verified.txt | 98 +++++++++ ...Type=String_implementations=2.verified.txt | 98 +++++++++ ...Type=String_implementations=4.verified.txt | 108 ++++++++++ ...Type=String_implementations=6.verified.txt | 108 ++++++++++ ...ngType=Guid_implementations=0.verified.txt | 73 +++++++ ...ngType=Guid_implementations=2.verified.txt | 73 +++++++ ...ngType=Guid_implementations=4.verified.txt | 74 +++++++ ...ngType=Guid_implementations=6.verified.txt | 74 +++++++ ...ingType=Int_implementations=0.verified.txt | 72 +++++++ ...ingType=Int_implementations=2.verified.txt | 72 +++++++ ...ingType=Int_implementations=4.verified.txt | 73 +++++++ ...ingType=Int_implementations=6.verified.txt | 73 +++++++ ...ngType=Long_implementations=0.verified.txt | 72 +++++++ ...ngType=Long_implementations=2.verified.txt | 72 +++++++ ...ngType=Long_implementations=4.verified.txt | 73 +++++++ ...ngType=Long_implementations=6.verified.txt | 73 +++++++ ...ransitNewId_implementations=0.verified.txt | 73 +++++++ ...ransitNewId_implementations=2.verified.txt | 73 +++++++ ...ransitNewId_implementations=4.verified.txt | 74 +++++++ ...ransitNewId_implementations=6.verified.txt | 74 +++++++ ...lableString_implementations=0.verified.txt | 94 +++++++++ ...lableString_implementations=2.verified.txt | 94 +++++++++ ...lableString_implementations=4.verified.txt | 104 +++++++++ ...lableString_implementations=6.verified.txt | 104 +++++++++ ...pe=ObjectId_implementations=0.verified.txt | 94 +++++++++ ...pe=ObjectId_implementations=2.verified.txt | 94 +++++++++ ...pe=ObjectId_implementations=4.verified.txt | 95 +++++++++ ...pe=ObjectId_implementations=6.verified.txt | 95 +++++++++ ...Type=String_implementations=0.verified.txt | 80 +++++++ ...Type=String_implementations=2.verified.txt | 80 +++++++ ...Type=String_implementations=4.verified.txt | 90 ++++++++ ...Type=String_implementations=6.verified.txt | 90 ++++++++ ...pe=ObjectId_implementations=0.verified.txt | 109 ++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 109 ++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 110 ++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 110 ++++++++++ ...ngType=Guid_implementations=0.verified.txt | 115 ++++++++++ ...ngType=Guid_implementations=2.verified.txt | 115 ++++++++++ ...ngType=Guid_implementations=4.verified.txt | 116 ++++++++++ ...ngType=Guid_implementations=6.verified.txt | 116 ++++++++++ ...ingType=Int_implementations=0.verified.txt | 114 ++++++++++ ...ingType=Int_implementations=2.verified.txt | 114 ++++++++++ ...ingType=Int_implementations=4.verified.txt | 115 ++++++++++ ...ingType=Int_implementations=6.verified.txt | 115 ++++++++++ ...ngType=Long_implementations=0.verified.txt | 116 ++++++++++ ...ngType=Long_implementations=2.verified.txt | 116 ++++++++++ ...ngType=Long_implementations=4.verified.txt | 117 +++++++++++ ...ngType=Long_implementations=6.verified.txt | 117 +++++++++++ ...ransitNewId_implementations=0.verified.txt | 123 +++++++++++ ...ransitNewId_implementations=2.verified.txt | 123 +++++++++++ ...ransitNewId_implementations=4.verified.txt | 124 +++++++++++ ...ransitNewId_implementations=6.verified.txt | 124 +++++++++++ ...lableString_implementations=0.verified.txt | 137 ++++++++++++ ...lableString_implementations=2.verified.txt | 137 ++++++++++++ ...lableString_implementations=4.verified.txt | 147 +++++++++++++ ...lableString_implementations=6.verified.txt | 147 +++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 137 ++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 137 ++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 138 ++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 138 ++++++++++++ ...Type=String_implementations=0.verified.txt | 118 +++++++++++ ...Type=String_implementations=2.verified.txt | 118 +++++++++++ ...Type=String_implementations=4.verified.txt | 128 +++++++++++ ...Type=String_implementations=6.verified.txt | 128 +++++++++++ ...ngType=Guid_implementations=0.verified.txt | 66 ++++++ ...ngType=Guid_implementations=2.verified.txt | 66 ++++++ ...ngType=Guid_implementations=4.verified.txt | 67 ++++++ ...ngType=Guid_implementations=6.verified.txt | 67 ++++++ ...ingType=Int_implementations=0.verified.txt | 65 ++++++ ...ingType=Int_implementations=2.verified.txt | 65 ++++++ ...ingType=Int_implementations=4.verified.txt | 66 ++++++ ...ingType=Int_implementations=6.verified.txt | 66 ++++++ ...ngType=Long_implementations=0.verified.txt | 65 ++++++ ...ngType=Long_implementations=2.verified.txt | 65 ++++++ ...ngType=Long_implementations=4.verified.txt | 66 ++++++ ...ngType=Long_implementations=6.verified.txt | 66 ++++++ ...ransitNewId_implementations=0.verified.txt | 66 ++++++ ...ransitNewId_implementations=2.verified.txt | 66 ++++++ ...ransitNewId_implementations=4.verified.txt | 67 ++++++ ...ransitNewId_implementations=6.verified.txt | 67 ++++++ ...lableString_implementations=0.verified.txt | 88 ++++++++ ...lableString_implementations=2.verified.txt | 88 ++++++++ ...lableString_implementations=4.verified.txt | 98 +++++++++ ...lableString_implementations=6.verified.txt | 98 +++++++++ ...pe=ObjectId_implementations=0.verified.txt | 89 ++++++++ ...pe=ObjectId_implementations=2.verified.txt | 89 ++++++++ ...pe=ObjectId_implementations=4.verified.txt | 90 ++++++++ ...pe=ObjectId_implementations=6.verified.txt | 90 ++++++++ ...Type=String_implementations=0.verified.txt | 74 +++++++ ...Type=String_implementations=2.verified.txt | 74 +++++++ ...Type=String_implementations=4.verified.txt | 84 ++++++++ ...Type=String_implementations=6.verified.txt | 84 ++++++++ ...ngType=Guid_implementations=0.verified.txt | 108 ++++++++++ ...ngType=Guid_implementations=2.verified.txt | 108 ++++++++++ ...ngType=Guid_implementations=4.verified.txt | 109 ++++++++++ ...ngType=Guid_implementations=6.verified.txt | 109 ++++++++++ ...ingType=Int_implementations=0.verified.txt | 107 ++++++++++ ...ingType=Int_implementations=2.verified.txt | 107 ++++++++++ ...ingType=Int_implementations=4.verified.txt | 108 ++++++++++ ...ingType=Int_implementations=6.verified.txt | 108 ++++++++++ ...ngType=Long_implementations=0.verified.txt | 109 ++++++++++ ...ngType=Long_implementations=2.verified.txt | 109 ++++++++++ ...ngType=Long_implementations=4.verified.txt | 110 ++++++++++ ...ngType=Long_implementations=6.verified.txt | 110 ++++++++++ ...ransitNewId_implementations=0.verified.txt | 116 ++++++++++ ...ransitNewId_implementations=2.verified.txt | 116 ++++++++++ ...ransitNewId_implementations=4.verified.txt | 117 +++++++++++ ...ransitNewId_implementations=6.verified.txt | 117 +++++++++++ ...lableString_implementations=0.verified.txt | 131 ++++++++++++ ...lableString_implementations=2.verified.txt | 131 ++++++++++++ ...lableString_implementations=4.verified.txt | 141 +++++++++++++ ...lableString_implementations=6.verified.txt | 141 +++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 132 ++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 132 ++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 133 ++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 133 ++++++++++++ ...Type=String_implementations=0.verified.txt | 112 ++++++++++ ...Type=String_implementations=2.verified.txt | 112 ++++++++++ ...Type=String_implementations=4.verified.txt | 122 +++++++++++ ...Type=String_implementations=6.verified.txt | 122 +++++++++++ ...ngType=Guid_implementations=0.verified.txt | 87 ++++++++ ...ngType=Guid_implementations=2.verified.txt | 87 ++++++++ ...ngType=Guid_implementations=4.verified.txt | 88 ++++++++ ...ngType=Guid_implementations=6.verified.txt | 88 ++++++++ ...ingType=Int_implementations=0.verified.txt | 86 ++++++++ ...ingType=Int_implementations=2.verified.txt | 86 ++++++++ ...ingType=Int_implementations=4.verified.txt | 87 ++++++++ ...ingType=Int_implementations=6.verified.txt | 87 ++++++++ ...ngType=Long_implementations=0.verified.txt | 86 ++++++++ ...ngType=Long_implementations=2.verified.txt | 86 ++++++++ ...ngType=Long_implementations=4.verified.txt | 87 ++++++++ ...ngType=Long_implementations=6.verified.txt | 87 ++++++++ ...ransitNewId_implementations=0.verified.txt | 87 ++++++++ ...ransitNewId_implementations=2.verified.txt | 87 ++++++++ ...ransitNewId_implementations=4.verified.txt | 88 ++++++++ ...ransitNewId_implementations=6.verified.txt | 88 ++++++++ ...lableString_implementations=0.verified.txt | 115 ++++++++++ ...lableString_implementations=2.verified.txt | 115 ++++++++++ ...lableString_implementations=4.verified.txt | 125 +++++++++++ ...lableString_implementations=6.verified.txt | 125 +++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 116 ++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 116 ++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 117 +++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 117 +++++++++++ ...Type=String_implementations=0.verified.txt | 94 +++++++++ ...Type=String_implementations=2.verified.txt | 94 +++++++++ ...Type=String_implementations=4.verified.txt | 104 +++++++++ ...Type=String_implementations=6.verified.txt | 104 +++++++++ ...ngType=Guid_implementations=0.verified.txt | 129 ++++++++++++ ...ngType=Guid_implementations=2.verified.txt | 129 ++++++++++++ ...ngType=Guid_implementations=4.verified.txt | 130 ++++++++++++ ...ngType=Guid_implementations=6.verified.txt | 130 ++++++++++++ ...ingType=Int_implementations=0.verified.txt | 128 +++++++++++ ...ingType=Int_implementations=2.verified.txt | 128 +++++++++++ ...ingType=Int_implementations=4.verified.txt | 129 ++++++++++++ ...ingType=Int_implementations=6.verified.txt | 129 ++++++++++++ ...ngType=Long_implementations=0.verified.txt | 130 ++++++++++++ ...ngType=Long_implementations=2.verified.txt | 130 ++++++++++++ ...ngType=Long_implementations=4.verified.txt | 131 ++++++++++++ ...ngType=Long_implementations=6.verified.txt | 131 ++++++++++++ ...ransitNewId_implementations=0.verified.txt | 137 ++++++++++++ ...ransitNewId_implementations=2.verified.txt | 137 ++++++++++++ ...ransitNewId_implementations=4.verified.txt | 138 ++++++++++++ ...ransitNewId_implementations=6.verified.txt | 138 ++++++++++++ ...lableString_implementations=0.verified.txt | 158 ++++++++++++++ ...lableString_implementations=2.verified.txt | 158 ++++++++++++++ ...lableString_implementations=4.verified.txt | 168 +++++++++++++++ ...lableString_implementations=6.verified.txt | 168 +++++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 159 ++++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 159 ++++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 160 ++++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 160 ++++++++++++++ ...Type=String_implementations=0.verified.txt | 132 ++++++++++++ ...Type=String_implementations=2.verified.txt | 132 ++++++++++++ ...Type=String_implementations=4.verified.txt | 142 +++++++++++++ ...Type=String_implementations=6.verified.txt | 142 +++++++++++++ ...ngType=Guid_implementations=0.verified.txt | 63 ++++++ ...ngType=Guid_implementations=2.verified.txt | 63 ++++++ ...ngType=Guid_implementations=4.verified.txt | 64 ++++++ ...ngType=Guid_implementations=6.verified.txt | 64 ++++++ ...ingType=Int_implementations=0.verified.txt | 62 ++++++ ...ingType=Int_implementations=2.verified.txt | 62 ++++++ ...ingType=Int_implementations=4.verified.txt | 63 ++++++ ...ingType=Int_implementations=6.verified.txt | 63 ++++++ ...ngType=Long_implementations=0.verified.txt | 62 ++++++ ...ngType=Long_implementations=2.verified.txt | 62 ++++++ ...ngType=Long_implementations=4.verified.txt | 63 ++++++ ...ngType=Long_implementations=6.verified.txt | 63 ++++++ ...ransitNewId_implementations=0.verified.txt | 63 ++++++ ...ransitNewId_implementations=2.verified.txt | 63 ++++++ ...ransitNewId_implementations=4.verified.txt | 64 ++++++ ...ransitNewId_implementations=6.verified.txt | 64 ++++++ ...lableString_implementations=0.verified.txt | 78 +++++++ ...lableString_implementations=2.verified.txt | 78 +++++++ ...lableString_implementations=4.verified.txt | 88 ++++++++ ...lableString_implementations=6.verified.txt | 88 ++++++++ ...pe=ObjectId_implementations=0.verified.txt | 78 +++++++ ...pe=ObjectId_implementations=2.verified.txt | 78 +++++++ ...pe=ObjectId_implementations=4.verified.txt | 79 +++++++ ...pe=ObjectId_implementations=6.verified.txt | 79 +++++++ ...Type=String_implementations=0.verified.txt | 71 +++++++ ...Type=String_implementations=2.verified.txt | 71 +++++++ ...Type=String_implementations=4.verified.txt | 81 +++++++ ...Type=String_implementations=6.verified.txt | 81 +++++++ ...ngType=Guid_implementations=0.verified.txt | 105 ++++++++++ ...ngType=Guid_implementations=2.verified.txt | 105 ++++++++++ ...ngType=Guid_implementations=4.verified.txt | 106 ++++++++++ ...ngType=Guid_implementations=6.verified.txt | 106 ++++++++++ ...ingType=Int_implementations=0.verified.txt | 104 +++++++++ ...ingType=Int_implementations=2.verified.txt | 104 +++++++++ ...ingType=Int_implementations=4.verified.txt | 105 ++++++++++ ...ingType=Int_implementations=6.verified.txt | 105 ++++++++++ ...ngType=Long_implementations=0.verified.txt | 106 ++++++++++ ...ngType=Long_implementations=2.verified.txt | 106 ++++++++++ ...ngType=Long_implementations=4.verified.txt | 107 ++++++++++ ...ngType=Long_implementations=6.verified.txt | 107 ++++++++++ ...ransitNewId_implementations=0.verified.txt | 113 ++++++++++ ...ransitNewId_implementations=2.verified.txt | 113 ++++++++++ ...ransitNewId_implementations=4.verified.txt | 114 ++++++++++ ...ransitNewId_implementations=6.verified.txt | 114 ++++++++++ ...lableString_implementations=0.verified.txt | 121 +++++++++++ ...lableString_implementations=2.verified.txt | 121 +++++++++++ ...lableString_implementations=4.verified.txt | 131 ++++++++++++ ...lableString_implementations=6.verified.txt | 131 ++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 121 +++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 121 +++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 122 +++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 122 +++++++++++ ...Type=String_implementations=0.verified.txt | 109 ++++++++++ ...Type=String_implementations=2.verified.txt | 109 ++++++++++ ...Type=String_implementations=4.verified.txt | 119 +++++++++++ ...Type=String_implementations=6.verified.txt | 119 +++++++++++ ...ngType=Guid_implementations=0.verified.txt | 84 ++++++++ ...ngType=Guid_implementations=2.verified.txt | 84 ++++++++ ...ngType=Guid_implementations=4.verified.txt | 85 ++++++++ ...ngType=Guid_implementations=6.verified.txt | 85 ++++++++ ...ingType=Int_implementations=0.verified.txt | 83 ++++++++ ...ingType=Int_implementations=2.verified.txt | 83 ++++++++ ...ingType=Int_implementations=4.verified.txt | 84 ++++++++ ...ingType=Int_implementations=6.verified.txt | 84 ++++++++ ...ngType=Long_implementations=0.verified.txt | 83 ++++++++ ...ngType=Long_implementations=2.verified.txt | 83 ++++++++ ...ngType=Long_implementations=4.verified.txt | 84 ++++++++ ...ngType=Long_implementations=6.verified.txt | 84 ++++++++ ...ransitNewId_implementations=0.verified.txt | 84 ++++++++ ...ransitNewId_implementations=2.verified.txt | 84 ++++++++ ...ransitNewId_implementations=4.verified.txt | 85 ++++++++ ...ransitNewId_implementations=6.verified.txt | 85 ++++++++ ...lableString_implementations=0.verified.txt | 105 ++++++++++ ...lableString_implementations=2.verified.txt | 105 ++++++++++ ...lableString_implementations=4.verified.txt | 115 ++++++++++ ...lableString_implementations=6.verified.txt | 115 ++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 105 ++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 105 ++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 106 ++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 106 ++++++++++ ...Type=String_implementations=0.verified.txt | 91 ++++++++ ...Type=String_implementations=2.verified.txt | 91 ++++++++ ...Type=String_implementations=4.verified.txt | 101 +++++++++ ...Type=String_implementations=6.verified.txt | 101 +++++++++ ...ngType=Guid_implementations=0.verified.txt | 126 +++++++++++ ...ngType=Guid_implementations=2.verified.txt | 126 +++++++++++ ...ngType=Guid_implementations=4.verified.txt | 127 +++++++++++ ...ngType=Guid_implementations=6.verified.txt | 127 +++++++++++ ...ingType=Int_implementations=0.verified.txt | 125 +++++++++++ ...ingType=Int_implementations=2.verified.txt | 125 +++++++++++ ...ingType=Int_implementations=4.verified.txt | 126 +++++++++++ ...ingType=Int_implementations=6.verified.txt | 126 +++++++++++ ...ngType=Long_implementations=0.verified.txt | 127 +++++++++++ ...ngType=Long_implementations=2.verified.txt | 127 +++++++++++ ...ngType=Long_implementations=4.verified.txt | 128 +++++++++++ ...ngType=Long_implementations=6.verified.txt | 128 +++++++++++ ...ransitNewId_implementations=0.verified.txt | 134 ++++++++++++ ...ransitNewId_implementations=2.verified.txt | 134 ++++++++++++ ...ransitNewId_implementations=4.verified.txt | 135 ++++++++++++ ...ransitNewId_implementations=6.verified.txt | 135 ++++++++++++ ...lableString_implementations=0.verified.txt | 148 +++++++++++++ ...lableString_implementations=2.verified.txt | 148 +++++++++++++ ...lableString_implementations=4.verified.txt | 158 ++++++++++++++ ...lableString_implementations=6.verified.txt | 158 ++++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 148 +++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 148 +++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 149 +++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 149 +++++++++++++ ...Type=String_implementations=0.verified.txt | 129 ++++++++++++ ...Type=String_implementations=2.verified.txt | 129 ++++++++++++ ...Type=String_implementations=4.verified.txt | 139 ++++++++++++ ...Type=String_implementations=6.verified.txt | 139 ++++++++++++ ...ngType=Guid_implementations=0.verified.txt | 77 +++++++ ...ngType=Guid_implementations=2.verified.txt | 77 +++++++ ...ngType=Guid_implementations=4.verified.txt | 78 +++++++ ...ngType=Guid_implementations=6.verified.txt | 78 +++++++ ...ingType=Int_implementations=0.verified.txt | 76 +++++++ ...ingType=Int_implementations=2.verified.txt | 76 +++++++ ...ingType=Int_implementations=4.verified.txt | 77 +++++++ ...ingType=Int_implementations=6.verified.txt | 77 +++++++ ...ngType=Long_implementations=0.verified.txt | 76 +++++++ ...ngType=Long_implementations=2.verified.txt | 76 +++++++ ...ngType=Long_implementations=4.verified.txt | 77 +++++++ ...ngType=Long_implementations=6.verified.txt | 77 +++++++ ...ransitNewId_implementations=0.verified.txt | 77 +++++++ ...ransitNewId_implementations=2.verified.txt | 77 +++++++ ...ransitNewId_implementations=4.verified.txt | 78 +++++++ ...ransitNewId_implementations=6.verified.txt | 78 +++++++ ...lableString_implementations=0.verified.txt | 99 +++++++++ ...lableString_implementations=2.verified.txt | 99 +++++++++ ...lableString_implementations=4.verified.txt | 109 ++++++++++ ...lableString_implementations=6.verified.txt | 109 ++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 100 +++++++++ ...pe=ObjectId_implementations=2.verified.txt | 100 +++++++++ ...pe=ObjectId_implementations=4.verified.txt | 101 +++++++++ ...pe=ObjectId_implementations=6.verified.txt | 101 +++++++++ ...Type=String_implementations=0.verified.txt | 85 ++++++++ ...Type=String_implementations=2.verified.txt | 85 ++++++++ ...Type=String_implementations=4.verified.txt | 95 +++++++++ ...Type=String_implementations=6.verified.txt | 95 +++++++++ ...pe=ObjectId_implementations=0.verified.txt | 61 ++++++ ...pe=ObjectId_implementations=2.verified.txt | 61 ++++++ ...pe=ObjectId_implementations=4.verified.txt | 62 ++++++ ...pe=ObjectId_implementations=6.verified.txt | 62 ++++++ ...ngType=Guid_implementations=0.verified.txt | 119 +++++++++++ ...ngType=Guid_implementations=2.verified.txt | 119 +++++++++++ ...ngType=Guid_implementations=4.verified.txt | 120 +++++++++++ ...ngType=Guid_implementations=6.verified.txt | 120 +++++++++++ ...ingType=Int_implementations=0.verified.txt | 118 +++++++++++ ...ingType=Int_implementations=2.verified.txt | 118 +++++++++++ ...ingType=Int_implementations=4.verified.txt | 119 +++++++++++ ...ingType=Int_implementations=6.verified.txt | 119 +++++++++++ ...ngType=Long_implementations=0.verified.txt | 120 +++++++++++ ...ngType=Long_implementations=2.verified.txt | 120 +++++++++++ ...ngType=Long_implementations=4.verified.txt | 121 +++++++++++ ...ngType=Long_implementations=6.verified.txt | 121 +++++++++++ ...ransitNewId_implementations=0.verified.txt | 127 +++++++++++ ...ransitNewId_implementations=2.verified.txt | 127 +++++++++++ ...ransitNewId_implementations=4.verified.txt | 128 +++++++++++ ...ransitNewId_implementations=6.verified.txt | 128 +++++++++++ ...lableString_implementations=0.verified.txt | 142 +++++++++++++ ...lableString_implementations=2.verified.txt | 142 +++++++++++++ ...lableString_implementations=4.verified.txt | 152 ++++++++++++++ ...lableString_implementations=6.verified.txt | 152 ++++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 143 +++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 143 +++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 144 +++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 144 +++++++++++++ ...Type=String_implementations=0.verified.txt | 123 +++++++++++ ...Type=String_implementations=2.verified.txt | 123 +++++++++++ ...Type=String_implementations=4.verified.txt | 133 ++++++++++++ ...Type=String_implementations=6.verified.txt | 133 ++++++++++++ ...ngType=Guid_implementations=0.verified.txt | 98 +++++++++ ...ngType=Guid_implementations=2.verified.txt | 98 +++++++++ ...ngType=Guid_implementations=4.verified.txt | 99 +++++++++ ...ngType=Guid_implementations=6.verified.txt | 99 +++++++++ ...ingType=Int_implementations=0.verified.txt | 97 +++++++++ ...ingType=Int_implementations=2.verified.txt | 97 +++++++++ ...ingType=Int_implementations=4.verified.txt | 98 +++++++++ ...ingType=Int_implementations=6.verified.txt | 98 +++++++++ ...ngType=Long_implementations=0.verified.txt | 97 +++++++++ ...ngType=Long_implementations=2.verified.txt | 97 +++++++++ ...ngType=Long_implementations=4.verified.txt | 98 +++++++++ ...ngType=Long_implementations=6.verified.txt | 98 +++++++++ ...ransitNewId_implementations=0.verified.txt | 98 +++++++++ ...ransitNewId_implementations=2.verified.txt | 98 +++++++++ ...ransitNewId_implementations=4.verified.txt | 99 +++++++++ ...ransitNewId_implementations=6.verified.txt | 99 +++++++++ ...lableString_implementations=0.verified.txt | 126 +++++++++++ ...lableString_implementations=2.verified.txt | 126 +++++++++++ ...lableString_implementations=4.verified.txt | 136 ++++++++++++ ...lableString_implementations=6.verified.txt | 136 ++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 127 +++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 127 +++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 128 +++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 128 +++++++++++ ...Type=String_implementations=0.verified.txt | 105 ++++++++++ ...Type=String_implementations=2.verified.txt | 105 ++++++++++ ...Type=String_implementations=4.verified.txt | 115 ++++++++++ ...Type=String_implementations=6.verified.txt | 115 ++++++++++ ...ngType=Guid_implementations=0.verified.txt | 140 +++++++++++++ ...ngType=Guid_implementations=2.verified.txt | 140 +++++++++++++ ...ngType=Guid_implementations=4.verified.txt | 141 +++++++++++++ ...ngType=Guid_implementations=6.verified.txt | 141 +++++++++++++ ...ingType=Int_implementations=0.verified.txt | 139 ++++++++++++ ...ingType=Int_implementations=2.verified.txt | 139 ++++++++++++ ...ingType=Int_implementations=4.verified.txt | 140 +++++++++++++ ...ingType=Int_implementations=6.verified.txt | 140 +++++++++++++ ...ngType=Long_implementations=0.verified.txt | 141 +++++++++++++ ...ngType=Long_implementations=2.verified.txt | 141 +++++++++++++ ...ngType=Long_implementations=4.verified.txt | 142 +++++++++++++ ...ngType=Long_implementations=6.verified.txt | 142 +++++++++++++ ...ransitNewId_implementations=0.verified.txt | 148 +++++++++++++ ...ransitNewId_implementations=2.verified.txt | 148 +++++++++++++ ...ransitNewId_implementations=4.verified.txt | 149 +++++++++++++ ...ransitNewId_implementations=6.verified.txt | 149 +++++++++++++ ...lableString_implementations=0.verified.txt | 169 +++++++++++++++ ...lableString_implementations=2.verified.txt | 169 +++++++++++++++ ...lableString_implementations=4.verified.txt | 179 ++++++++++++++++ ...lableString_implementations=6.verified.txt | 179 ++++++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 170 +++++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 170 +++++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 171 +++++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 171 +++++++++++++++ ...Type=String_implementations=0.verified.txt | 143 +++++++++++++ ...Type=String_implementations=2.verified.txt | 143 +++++++++++++ ...Type=String_implementations=4.verified.txt | 153 ++++++++++++++ ...Type=String_implementations=6.verified.txt | 153 ++++++++++++++ ...ngType=Guid_implementations=0.verified.txt | 70 +++++++ ...ngType=Guid_implementations=2.verified.txt | 70 +++++++ ...ngType=Guid_implementations=4.verified.txt | 71 +++++++ ...ngType=Guid_implementations=6.verified.txt | 71 +++++++ ...ingType=Int_implementations=0.verified.txt | 70 +++++++ ...ingType=Int_implementations=2.verified.txt | 70 +++++++ ...ingType=Int_implementations=4.verified.txt | 71 +++++++ ...ingType=Int_implementations=6.verified.txt | 71 +++++++ ...ngType=Long_implementations=0.verified.txt | 71 +++++++ ...ngType=Long_implementations=2.verified.txt | 71 +++++++ ...ngType=Long_implementations=4.verified.txt | 72 +++++++ ...ngType=Long_implementations=6.verified.txt | 72 +++++++ ...ransitNewId_implementations=0.verified.txt | 70 +++++++ ...ransitNewId_implementations=2.verified.txt | 70 +++++++ ...ransitNewId_implementations=4.verified.txt | 71 +++++++ ...ransitNewId_implementations=6.verified.txt | 71 +++++++ ...lableString_implementations=0.verified.txt | 86 ++++++++ ...lableString_implementations=2.verified.txt | 86 ++++++++ ...lableString_implementations=4.verified.txt | 96 +++++++++ ...lableString_implementations=6.verified.txt | 96 +++++++++ ...pe=ObjectId_implementations=0.verified.txt | 85 ++++++++ ...pe=ObjectId_implementations=2.verified.txt | 85 ++++++++ ...pe=ObjectId_implementations=4.verified.txt | 86 ++++++++ ...pe=ObjectId_implementations=6.verified.txt | 86 ++++++++ ...Type=String_implementations=0.verified.txt | 77 +++++++ ...Type=String_implementations=2.verified.txt | 77 +++++++ ...Type=String_implementations=4.verified.txt | 87 ++++++++ ...Type=String_implementations=6.verified.txt | 87 ++++++++ ...ngType=Guid_implementations=0.verified.txt | 112 ++++++++++ ...ngType=Guid_implementations=2.verified.txt | 112 ++++++++++ ...ngType=Guid_implementations=4.verified.txt | 113 ++++++++++ ...ngType=Guid_implementations=6.verified.txt | 113 ++++++++++ ...ingType=Int_implementations=0.verified.txt | 112 ++++++++++ ...ingType=Int_implementations=2.verified.txt | 112 ++++++++++ ...ingType=Int_implementations=4.verified.txt | 113 ++++++++++ ...ingType=Int_implementations=6.verified.txt | 113 ++++++++++ ...ngType=Long_implementations=0.verified.txt | 115 ++++++++++ ...ngType=Long_implementations=2.verified.txt | 115 ++++++++++ ...ngType=Long_implementations=4.verified.txt | 116 ++++++++++ ...ngType=Long_implementations=6.verified.txt | 116 ++++++++++ ...ransitNewId_implementations=0.verified.txt | 120 +++++++++++ ...ransitNewId_implementations=2.verified.txt | 120 +++++++++++ ...ransitNewId_implementations=4.verified.txt | 121 +++++++++++ ...ransitNewId_implementations=6.verified.txt | 121 +++++++++++ ...lableString_implementations=0.verified.txt | 129 ++++++++++++ ...lableString_implementations=2.verified.txt | 129 ++++++++++++ ...lableString_implementations=4.verified.txt | 139 ++++++++++++ ...lableString_implementations=6.verified.txt | 139 ++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 128 +++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 128 +++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 129 ++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 129 ++++++++++++ ...Type=String_implementations=0.verified.txt | 115 ++++++++++ ...Type=String_implementations=2.verified.txt | 115 ++++++++++ ...Type=String_implementations=4.verified.txt | 125 +++++++++++ ...Type=String_implementations=6.verified.txt | 125 +++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 36 ++++ ...pe=ObjectId_implementations=2.verified.txt | 36 ++++ ...pe=ObjectId_implementations=4.verified.txt | 37 ++++ ...pe=ObjectId_implementations=6.verified.txt | 37 ++++ ...ngType=Guid_implementations=0.verified.txt | 88 ++++++++ ...ngType=Guid_implementations=2.verified.txt | 88 ++++++++ ...ngType=Guid_implementations=4.verified.txt | 89 ++++++++ ...ngType=Guid_implementations=6.verified.txt | 89 ++++++++ ...ingType=Int_implementations=0.verified.txt | 88 ++++++++ ...ingType=Int_implementations=2.verified.txt | 88 ++++++++ ...ingType=Int_implementations=4.verified.txt | 89 ++++++++ ...ingType=Int_implementations=6.verified.txt | 89 ++++++++ ...ngType=Long_implementations=0.verified.txt | 89 ++++++++ ...ngType=Long_implementations=2.verified.txt | 89 ++++++++ ...ngType=Long_implementations=4.verified.txt | 90 ++++++++ ...ngType=Long_implementations=6.verified.txt | 90 ++++++++ ...ransitNewId_implementations=0.verified.txt | 88 ++++++++ ...ransitNewId_implementations=2.verified.txt | 88 ++++++++ ...ransitNewId_implementations=4.verified.txt | 89 ++++++++ ...ransitNewId_implementations=6.verified.txt | 89 ++++++++ ...lableString_implementations=0.verified.txt | 110 ++++++++++ ...lableString_implementations=2.verified.txt | 110 ++++++++++ ...lableString_implementations=4.verified.txt | 120 +++++++++++ ...lableString_implementations=6.verified.txt | 120 +++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 109 ++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 109 ++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 110 ++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 110 ++++++++++ ...Type=String_implementations=0.verified.txt | 94 +++++++++ ...Type=String_implementations=2.verified.txt | 94 +++++++++ ...Type=String_implementations=4.verified.txt | 104 +++++++++ ...Type=String_implementations=6.verified.txt | 104 +++++++++ ...ngType=Guid_implementations=0.verified.txt | 130 ++++++++++++ ...ngType=Guid_implementations=2.verified.txt | 130 ++++++++++++ ...ngType=Guid_implementations=4.verified.txt | 131 ++++++++++++ ...ngType=Guid_implementations=6.verified.txt | 131 ++++++++++++ ...ingType=Int_implementations=0.verified.txt | 130 ++++++++++++ ...ingType=Int_implementations=2.verified.txt | 130 ++++++++++++ ...ingType=Int_implementations=4.verified.txt | 131 ++++++++++++ ...ingType=Int_implementations=6.verified.txt | 131 ++++++++++++ ...ngType=Long_implementations=0.verified.txt | 133 ++++++++++++ ...ngType=Long_implementations=2.verified.txt | 133 ++++++++++++ ...ngType=Long_implementations=4.verified.txt | 134 ++++++++++++ ...ngType=Long_implementations=6.verified.txt | 134 ++++++++++++ ...ransitNewId_implementations=0.verified.txt | 138 ++++++++++++ ...ransitNewId_implementations=2.verified.txt | 138 ++++++++++++ ...ransitNewId_implementations=4.verified.txt | 139 ++++++++++++ ...ransitNewId_implementations=6.verified.txt | 139 ++++++++++++ ...lableString_implementations=0.verified.txt | 153 ++++++++++++++ ...lableString_implementations=2.verified.txt | 153 ++++++++++++++ ...lableString_implementations=4.verified.txt | 163 ++++++++++++++ ...lableString_implementations=6.verified.txt | 163 ++++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 152 ++++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 152 ++++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 153 ++++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 153 ++++++++++++++ ...Type=String_implementations=0.verified.txt | 132 ++++++++++++ ...Type=String_implementations=2.verified.txt | 132 ++++++++++++ ...Type=String_implementations=4.verified.txt | 142 +++++++++++++ ...Type=String_implementations=6.verified.txt | 142 +++++++++++++ ...ngType=Guid_implementations=0.verified.txt | 81 +++++++ ...ngType=Guid_implementations=2.verified.txt | 81 +++++++ ...ngType=Guid_implementations=4.verified.txt | 82 ++++++++ ...ngType=Guid_implementations=6.verified.txt | 82 ++++++++ ...ingType=Int_implementations=0.verified.txt | 81 +++++++ ...ingType=Int_implementations=2.verified.txt | 81 +++++++ ...ingType=Int_implementations=4.verified.txt | 82 ++++++++ ...ingType=Int_implementations=6.verified.txt | 82 ++++++++ ...ngType=Long_implementations=0.verified.txt | 82 ++++++++ ...ngType=Long_implementations=2.verified.txt | 82 ++++++++ ...ngType=Long_implementations=4.verified.txt | 83 ++++++++ ...ngType=Long_implementations=6.verified.txt | 83 ++++++++ ...ransitNewId_implementations=0.verified.txt | 81 +++++++ ...ransitNewId_implementations=2.verified.txt | 81 +++++++ ...ransitNewId_implementations=4.verified.txt | 82 ++++++++ ...ransitNewId_implementations=6.verified.txt | 82 ++++++++ ...lableString_implementations=0.verified.txt | 104 +++++++++ ...lableString_implementations=2.verified.txt | 104 +++++++++ ...lableString_implementations=4.verified.txt | 114 ++++++++++ ...lableString_implementations=6.verified.txt | 114 ++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 104 +++++++++ ...pe=ObjectId_implementations=2.verified.txt | 104 +++++++++ ...pe=ObjectId_implementations=4.verified.txt | 105 ++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 105 ++++++++++ ...Type=String_implementations=0.verified.txt | 88 ++++++++ ...Type=String_implementations=2.verified.txt | 88 ++++++++ ...Type=String_implementations=4.verified.txt | 98 +++++++++ ...Type=String_implementations=6.verified.txt | 98 +++++++++ ...ngType=Guid_implementations=0.verified.txt | 123 +++++++++++ ...ngType=Guid_implementations=2.verified.txt | 123 +++++++++++ ...ngType=Guid_implementations=4.verified.txt | 124 +++++++++++ ...ngType=Guid_implementations=6.verified.txt | 124 +++++++++++ ...ingType=Int_implementations=0.verified.txt | 123 +++++++++++ ...ingType=Int_implementations=2.verified.txt | 123 +++++++++++ ...ingType=Int_implementations=4.verified.txt | 124 +++++++++++ ...ingType=Int_implementations=6.verified.txt | 124 +++++++++++ ...ngType=Long_implementations=0.verified.txt | 126 +++++++++++ ...ngType=Long_implementations=2.verified.txt | 126 +++++++++++ ...ngType=Long_implementations=4.verified.txt | 127 +++++++++++ ...ngType=Long_implementations=6.verified.txt | 127 +++++++++++ ...ransitNewId_implementations=0.verified.txt | 131 ++++++++++++ ...ransitNewId_implementations=2.verified.txt | 131 ++++++++++++ ...ransitNewId_implementations=4.verified.txt | 132 ++++++++++++ ...ransitNewId_implementations=6.verified.txt | 132 ++++++++++++ ...lableString_implementations=0.verified.txt | 147 +++++++++++++ ...lableString_implementations=2.verified.txt | 147 +++++++++++++ ...lableString_implementations=4.verified.txt | 157 ++++++++++++++ ...lableString_implementations=6.verified.txt | 157 ++++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 147 +++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 147 +++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 148 +++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 148 +++++++++++++ ...Type=String_implementations=0.verified.txt | 126 +++++++++++ ...Type=String_implementations=2.verified.txt | 126 +++++++++++ ...Type=String_implementations=4.verified.txt | 136 ++++++++++++ ...Type=String_implementations=6.verified.txt | 136 ++++++++++++ ...ngType=Guid_implementations=0.verified.txt | 102 +++++++++ ...ngType=Guid_implementations=2.verified.txt | 102 +++++++++ ...ngType=Guid_implementations=4.verified.txt | 103 +++++++++ ...ngType=Guid_implementations=6.verified.txt | 103 +++++++++ ...ingType=Int_implementations=0.verified.txt | 102 +++++++++ ...ingType=Int_implementations=2.verified.txt | 102 +++++++++ ...ingType=Int_implementations=4.verified.txt | 103 +++++++++ ...ingType=Int_implementations=6.verified.txt | 103 +++++++++ ...ngType=Long_implementations=0.verified.txt | 103 +++++++++ ...ngType=Long_implementations=2.verified.txt | 103 +++++++++ ...ngType=Long_implementations=4.verified.txt | 104 +++++++++ ...ngType=Long_implementations=6.verified.txt | 104 +++++++++ ...ransitNewId_implementations=0.verified.txt | 102 +++++++++ ...ransitNewId_implementations=2.verified.txt | 102 +++++++++ ...ransitNewId_implementations=4.verified.txt | 103 +++++++++ ...ransitNewId_implementations=6.verified.txt | 103 +++++++++ ...lableString_implementations=0.verified.txt | 131 ++++++++++++ ...lableString_implementations=2.verified.txt | 131 ++++++++++++ ...lableString_implementations=4.verified.txt | 141 +++++++++++++ ...lableString_implementations=6.verified.txt | 141 +++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 131 ++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 131 ++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 132 ++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 132 ++++++++++++ ...Type=String_implementations=0.verified.txt | 108 ++++++++++ ...Type=String_implementations=2.verified.txt | 108 ++++++++++ ...Type=String_implementations=4.verified.txt | 118 +++++++++++ ...Type=String_implementations=6.verified.txt | 118 +++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 101 +++++++++ ...pe=ObjectId_implementations=2.verified.txt | 101 +++++++++ ...pe=ObjectId_implementations=4.verified.txt | 102 +++++++++ ...pe=ObjectId_implementations=6.verified.txt | 102 +++++++++ ...ngType=Guid_implementations=0.verified.txt | 144 +++++++++++++ ...ngType=Guid_implementations=2.verified.txt | 144 +++++++++++++ ...ngType=Guid_implementations=4.verified.txt | 145 +++++++++++++ ...ngType=Guid_implementations=6.verified.txt | 145 +++++++++++++ ...ingType=Int_implementations=0.verified.txt | 144 +++++++++++++ ...ingType=Int_implementations=2.verified.txt | 144 +++++++++++++ ...ingType=Int_implementations=4.verified.txt | 145 +++++++++++++ ...ingType=Int_implementations=6.verified.txt | 145 +++++++++++++ ...ngType=Long_implementations=0.verified.txt | 147 +++++++++++++ ...ngType=Long_implementations=2.verified.txt | 147 +++++++++++++ ...ngType=Long_implementations=4.verified.txt | 148 +++++++++++++ ...ngType=Long_implementations=6.verified.txt | 148 +++++++++++++ ...ransitNewId_implementations=0.verified.txt | 152 ++++++++++++++ ...ransitNewId_implementations=2.verified.txt | 152 ++++++++++++++ ...ransitNewId_implementations=4.verified.txt | 153 ++++++++++++++ ...ransitNewId_implementations=6.verified.txt | 153 ++++++++++++++ ...lableString_implementations=0.verified.txt | 174 +++++++++++++++ ...lableString_implementations=2.verified.txt | 174 +++++++++++++++ ...lableString_implementations=4.verified.txt | 184 ++++++++++++++++ ...lableString_implementations=6.verified.txt | 184 ++++++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 174 +++++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 174 +++++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 175 ++++++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 175 ++++++++++++++++ ...Type=String_implementations=0.verified.txt | 146 +++++++++++++ ...Type=String_implementations=2.verified.txt | 146 +++++++++++++ ...Type=String_implementations=4.verified.txt | 156 ++++++++++++++ ...Type=String_implementations=6.verified.txt | 156 ++++++++++++++ ...ngType=Guid_implementations=0.verified.txt | 78 +++++++ ...ngType=Guid_implementations=2.verified.txt | 78 +++++++ ...ngType=Guid_implementations=4.verified.txt | 79 +++++++ ...ngType=Guid_implementations=6.verified.txt | 79 +++++++ ...ingType=Int_implementations=0.verified.txt | 78 +++++++ ...ingType=Int_implementations=2.verified.txt | 78 +++++++ ...ingType=Int_implementations=4.verified.txt | 79 +++++++ ...ingType=Int_implementations=6.verified.txt | 79 +++++++ ...ngType=Long_implementations=0.verified.txt | 79 +++++++ ...ngType=Long_implementations=2.verified.txt | 79 +++++++ ...ngType=Long_implementations=4.verified.txt | 80 +++++++ ...ngType=Long_implementations=6.verified.txt | 80 +++++++ ...ransitNewId_implementations=0.verified.txt | 78 +++++++ ...ransitNewId_implementations=2.verified.txt | 78 +++++++ ...ransitNewId_implementations=4.verified.txt | 79 +++++++ ...ransitNewId_implementations=6.verified.txt | 79 +++++++ ...lableString_implementations=0.verified.txt | 94 +++++++++ ...lableString_implementations=2.verified.txt | 94 +++++++++ ...lableString_implementations=4.verified.txt | 104 +++++++++ ...lableString_implementations=6.verified.txt | 104 +++++++++ ...pe=ObjectId_implementations=0.verified.txt | 93 ++++++++ ...pe=ObjectId_implementations=2.verified.txt | 93 ++++++++ ...pe=ObjectId_implementations=4.verified.txt | 94 +++++++++ ...pe=ObjectId_implementations=6.verified.txt | 94 +++++++++ ...Type=String_implementations=0.verified.txt | 85 ++++++++ ...Type=String_implementations=2.verified.txt | 85 ++++++++ ...Type=String_implementations=4.verified.txt | 95 +++++++++ ...Type=String_implementations=6.verified.txt | 95 +++++++++ ...ngType=Guid_implementations=0.verified.txt | 120 +++++++++++ ...ngType=Guid_implementations=2.verified.txt | 120 +++++++++++ ...ngType=Guid_implementations=4.verified.txt | 121 +++++++++++ ...ngType=Guid_implementations=6.verified.txt | 121 +++++++++++ ...ingType=Int_implementations=0.verified.txt | 120 +++++++++++ ...ingType=Int_implementations=2.verified.txt | 120 +++++++++++ ...ingType=Int_implementations=4.verified.txt | 121 +++++++++++ ...ingType=Int_implementations=6.verified.txt | 121 +++++++++++ ...ngType=Long_implementations=0.verified.txt | 123 +++++++++++ ...ngType=Long_implementations=2.verified.txt | 123 +++++++++++ ...ngType=Long_implementations=4.verified.txt | 124 +++++++++++ ...ngType=Long_implementations=6.verified.txt | 124 +++++++++++ ...ransitNewId_implementations=0.verified.txt | 128 +++++++++++ ...ransitNewId_implementations=2.verified.txt | 128 +++++++++++ ...ransitNewId_implementations=4.verified.txt | 129 ++++++++++++ ...ransitNewId_implementations=6.verified.txt | 129 ++++++++++++ ...lableString_implementations=0.verified.txt | 137 ++++++++++++ ...lableString_implementations=2.verified.txt | 137 ++++++++++++ ...lableString_implementations=4.verified.txt | 147 +++++++++++++ ...lableString_implementations=6.verified.txt | 147 +++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 136 ++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 136 ++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 137 ++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 137 ++++++++++++ ...Type=String_implementations=0.verified.txt | 123 +++++++++++ ...Type=String_implementations=2.verified.txt | 123 +++++++++++ ...Type=String_implementations=4.verified.txt | 133 ++++++++++++ ...Type=String_implementations=6.verified.txt | 133 ++++++++++++ ...ngType=Guid_implementations=0.verified.txt | 99 +++++++++ ...ngType=Guid_implementations=2.verified.txt | 99 +++++++++ ...ngType=Guid_implementations=4.verified.txt | 100 +++++++++ ...ngType=Guid_implementations=6.verified.txt | 100 +++++++++ ...ingType=Int_implementations=0.verified.txt | 99 +++++++++ ...ingType=Int_implementations=2.verified.txt | 99 +++++++++ ...ingType=Int_implementations=4.verified.txt | 100 +++++++++ ...ingType=Int_implementations=6.verified.txt | 100 +++++++++ ...ngType=Long_implementations=0.verified.txt | 100 +++++++++ ...ngType=Long_implementations=2.verified.txt | 100 +++++++++ ...ngType=Long_implementations=4.verified.txt | 101 +++++++++ ...ngType=Long_implementations=6.verified.txt | 101 +++++++++ ...ransitNewId_implementations=0.verified.txt | 99 +++++++++ ...ransitNewId_implementations=2.verified.txt | 99 +++++++++ ...ransitNewId_implementations=4.verified.txt | 100 +++++++++ ...ransitNewId_implementations=6.verified.txt | 100 +++++++++ ...lableString_implementations=0.verified.txt | 121 +++++++++++ ...lableString_implementations=2.verified.txt | 121 +++++++++++ ...lableString_implementations=4.verified.txt | 131 ++++++++++++ ...lableString_implementations=6.verified.txt | 131 ++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 120 +++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 120 +++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 121 +++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 121 +++++++++++ ...Type=String_implementations=0.verified.txt | 105 ++++++++++ ...Type=String_implementations=2.verified.txt | 105 ++++++++++ ...Type=String_implementations=4.verified.txt | 115 ++++++++++ ...Type=String_implementations=6.verified.txt | 115 ++++++++++ ...ngType=Guid_implementations=0.verified.txt | 141 +++++++++++++ ...ngType=Guid_implementations=2.verified.txt | 141 +++++++++++++ ...ngType=Guid_implementations=4.verified.txt | 142 +++++++++++++ ...ngType=Guid_implementations=6.verified.txt | 142 +++++++++++++ ...ingType=Int_implementations=0.verified.txt | 141 +++++++++++++ ...ingType=Int_implementations=2.verified.txt | 141 +++++++++++++ ...ingType=Int_implementations=4.verified.txt | 142 +++++++++++++ ...ingType=Int_implementations=6.verified.txt | 142 +++++++++++++ ...ngType=Long_implementations=0.verified.txt | 144 +++++++++++++ ...ngType=Long_implementations=2.verified.txt | 144 +++++++++++++ ...ngType=Long_implementations=4.verified.txt | 145 +++++++++++++ ...ngType=Long_implementations=6.verified.txt | 145 +++++++++++++ ...ransitNewId_implementations=0.verified.txt | 149 +++++++++++++ ...ransitNewId_implementations=2.verified.txt | 149 +++++++++++++ ...ransitNewId_implementations=4.verified.txt | 150 +++++++++++++ ...ransitNewId_implementations=6.verified.txt | 150 +++++++++++++ ...lableString_implementations=0.verified.txt | 164 +++++++++++++++ ...lableString_implementations=2.verified.txt | 164 +++++++++++++++ ...lableString_implementations=4.verified.txt | 174 +++++++++++++++ ...lableString_implementations=6.verified.txt | 174 +++++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 163 ++++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 163 ++++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 164 +++++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 164 +++++++++++++++ ...Type=String_implementations=0.verified.txt | 143 +++++++++++++ ...Type=String_implementations=2.verified.txt | 143 +++++++++++++ ...Type=String_implementations=4.verified.txt | 153 ++++++++++++++ ...Type=String_implementations=6.verified.txt | 153 ++++++++++++++ ...ngType=Guid_implementations=0.verified.txt | 92 ++++++++ ...ngType=Guid_implementations=2.verified.txt | 92 ++++++++ ...ngType=Guid_implementations=4.verified.txt | 93 ++++++++ ...ngType=Guid_implementations=6.verified.txt | 93 ++++++++ ...ingType=Int_implementations=0.verified.txt | 92 ++++++++ ...ingType=Int_implementations=2.verified.txt | 92 ++++++++ ...ingType=Int_implementations=4.verified.txt | 93 ++++++++ ...ingType=Int_implementations=6.verified.txt | 93 ++++++++ ...ngType=Long_implementations=0.verified.txt | 93 ++++++++ ...ngType=Long_implementations=2.verified.txt | 93 ++++++++ ...ngType=Long_implementations=4.verified.txt | 94 +++++++++ ...ngType=Long_implementations=6.verified.txt | 94 +++++++++ ...ransitNewId_implementations=0.verified.txt | 92 ++++++++ ...ransitNewId_implementations=2.verified.txt | 92 ++++++++ ...ransitNewId_implementations=4.verified.txt | 93 ++++++++ ...ransitNewId_implementations=6.verified.txt | 93 ++++++++ ...lableString_implementations=0.verified.txt | 115 ++++++++++ ...lableString_implementations=2.verified.txt | 115 ++++++++++ ...lableString_implementations=4.verified.txt | 125 +++++++++++ ...lableString_implementations=6.verified.txt | 125 +++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 115 ++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 115 ++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 116 ++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 116 ++++++++++ ...Type=String_implementations=0.verified.txt | 99 +++++++++ ...Type=String_implementations=2.verified.txt | 99 +++++++++ ...Type=String_implementations=4.verified.txt | 109 ++++++++++ ...Type=String_implementations=6.verified.txt | 109 ++++++++++ ...ngType=Guid_implementations=0.verified.txt | 134 ++++++++++++ ...ngType=Guid_implementations=2.verified.txt | 134 ++++++++++++ ...ngType=Guid_implementations=4.verified.txt | 135 ++++++++++++ ...ngType=Guid_implementations=6.verified.txt | 135 ++++++++++++ ...ingType=Int_implementations=0.verified.txt | 134 ++++++++++++ ...ingType=Int_implementations=2.verified.txt | 134 ++++++++++++ ...ingType=Int_implementations=4.verified.txt | 135 ++++++++++++ ...ingType=Int_implementations=6.verified.txt | 135 ++++++++++++ ...ngType=Long_implementations=0.verified.txt | 137 ++++++++++++ ...ngType=Long_implementations=2.verified.txt | 137 ++++++++++++ ...ngType=Long_implementations=4.verified.txt | 138 ++++++++++++ ...ngType=Long_implementations=6.verified.txt | 138 ++++++++++++ ...ransitNewId_implementations=0.verified.txt | 142 +++++++++++++ ...ransitNewId_implementations=2.verified.txt | 142 +++++++++++++ ...ransitNewId_implementations=4.verified.txt | 143 +++++++++++++ ...ransitNewId_implementations=6.verified.txt | 143 +++++++++++++ ...lableString_implementations=0.verified.txt | 158 ++++++++++++++ ...lableString_implementations=2.verified.txt | 158 ++++++++++++++ ...lableString_implementations=4.verified.txt | 168 +++++++++++++++ ...lableString_implementations=6.verified.txt | 168 +++++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 158 ++++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 158 ++++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 159 ++++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 159 ++++++++++++++ ...Type=String_implementations=0.verified.txt | 137 ++++++++++++ ...Type=String_implementations=2.verified.txt | 137 ++++++++++++ ...Type=String_implementations=4.verified.txt | 147 +++++++++++++ ...Type=String_implementations=6.verified.txt | 147 +++++++++++++ ...ngType=Guid_implementations=0.verified.txt | 113 ++++++++++ ...ngType=Guid_implementations=2.verified.txt | 113 ++++++++++ ...ngType=Guid_implementations=4.verified.txt | 114 ++++++++++ ...ngType=Guid_implementations=6.verified.txt | 114 ++++++++++ ...ingType=Int_implementations=0.verified.txt | 113 ++++++++++ ...ingType=Int_implementations=2.verified.txt | 113 ++++++++++ ...ingType=Int_implementations=4.verified.txt | 114 ++++++++++ ...ingType=Int_implementations=6.verified.txt | 114 ++++++++++ ...ngType=Long_implementations=0.verified.txt | 114 ++++++++++ ...ngType=Long_implementations=2.verified.txt | 114 ++++++++++ ...ngType=Long_implementations=4.verified.txt | 115 ++++++++++ ...ngType=Long_implementations=6.verified.txt | 115 ++++++++++ ...ransitNewId_implementations=0.verified.txt | 113 ++++++++++ ...ransitNewId_implementations=2.verified.txt | 113 ++++++++++ ...ransitNewId_implementations=4.verified.txt | 114 ++++++++++ ...ransitNewId_implementations=6.verified.txt | 114 ++++++++++ ...lableString_implementations=0.verified.txt | 142 +++++++++++++ ...lableString_implementations=2.verified.txt | 142 +++++++++++++ ...lableString_implementations=4.verified.txt | 152 ++++++++++++++ ...lableString_implementations=6.verified.txt | 152 ++++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 142 +++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 142 +++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 143 +++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 143 +++++++++++++ ...Type=String_implementations=0.verified.txt | 119 +++++++++++ ...Type=String_implementations=2.verified.txt | 119 +++++++++++ ...Type=String_implementations=4.verified.txt | 129 ++++++++++++ ...Type=String_implementations=6.verified.txt | 129 ++++++++++++ ...ngType=Guid_implementations=0.verified.txt | 155 ++++++++++++++ ...ngType=Guid_implementations=2.verified.txt | 155 ++++++++++++++ ...ngType=Guid_implementations=4.verified.txt | 156 ++++++++++++++ ...ngType=Guid_implementations=6.verified.txt | 156 ++++++++++++++ ...ingType=Int_implementations=0.verified.txt | 155 ++++++++++++++ ...ingType=Int_implementations=2.verified.txt | 155 ++++++++++++++ ...ingType=Int_implementations=4.verified.txt | 156 ++++++++++++++ ...ingType=Int_implementations=6.verified.txt | 156 ++++++++++++++ ...ngType=Long_implementations=0.verified.txt | 158 ++++++++++++++ ...ngType=Long_implementations=2.verified.txt | 158 ++++++++++++++ ...ngType=Long_implementations=4.verified.txt | 159 ++++++++++++++ ...ngType=Long_implementations=6.verified.txt | 159 ++++++++++++++ ...ransitNewId_implementations=0.verified.txt | 163 ++++++++++++++ ...ransitNewId_implementations=2.verified.txt | 163 ++++++++++++++ ...ransitNewId_implementations=4.verified.txt | 164 +++++++++++++++ ...ransitNewId_implementations=6.verified.txt | 164 +++++++++++++++ ...lableString_implementations=0.verified.txt | 185 ++++++++++++++++ ...lableString_implementations=2.verified.txt | 185 ++++++++++++++++ ...lableString_implementations=4.verified.txt | 195 +++++++++++++++++ ...lableString_implementations=6.verified.txt | 195 +++++++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 185 ++++++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 185 ++++++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 186 ++++++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 186 ++++++++++++++++ ...Type=String_implementations=0.verified.txt | 157 ++++++++++++++ ...Type=String_implementations=2.verified.txt | 157 ++++++++++++++ ...Type=String_implementations=4.verified.txt | 167 +++++++++++++++ ...Type=String_implementations=6.verified.txt | 167 +++++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 85 ++++++++ ...pe=ObjectId_implementations=2.verified.txt | 85 ++++++++ ...pe=ObjectId_implementations=4.verified.txt | 86 ++++++++ ...pe=ObjectId_implementations=6.verified.txt | 86 ++++++++ ...pe=ObjectId_implementations=0.verified.txt | 128 +++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 128 +++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 129 ++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 129 ++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 47 +++++ ...pe=ObjectId_implementations=2.verified.txt | 47 +++++ ...pe=ObjectId_implementations=4.verified.txt | 48 +++++ ...pe=ObjectId_implementations=6.verified.txt | 48 +++++ ...pe=ObjectId_implementations=0.verified.txt | 90 ++++++++ ...pe=ObjectId_implementations=2.verified.txt | 90 ++++++++ ...pe=ObjectId_implementations=4.verified.txt | 91 ++++++++ ...pe=ObjectId_implementations=6.verified.txt | 91 ++++++++ ...pe=ObjectId_implementations=0.verified.txt | 74 +++++++ ...pe=ObjectId_implementations=2.verified.txt | 74 +++++++ ...pe=ObjectId_implementations=4.verified.txt | 75 +++++++ ...pe=ObjectId_implementations=6.verified.txt | 75 +++++++ ...pe=ObjectId_implementations=0.verified.txt | 117 +++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 117 +++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 118 +++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 118 +++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 69 ++++++ ...pe=ObjectId_implementations=2.verified.txt | 69 ++++++ ...pe=ObjectId_implementations=4.verified.txt | 70 +++++++ ...pe=ObjectId_implementations=6.verified.txt | 70 +++++++ ...pe=ObjectId_implementations=0.verified.txt | 112 ++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 112 ++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 113 ++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 113 ++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 96 +++++++++ ...pe=ObjectId_implementations=2.verified.txt | 96 +++++++++ ...pe=ObjectId_implementations=4.verified.txt | 97 +++++++++ ...pe=ObjectId_implementations=6.verified.txt | 97 +++++++++ ...pe=ObjectId_implementations=0.verified.txt | 79 +++++++ ...pe=ObjectId_implementations=2.verified.txt | 79 +++++++ ...pe=ObjectId_implementations=4.verified.txt | 80 +++++++ ...pe=ObjectId_implementations=6.verified.txt | 80 +++++++ ...pe=ObjectId_implementations=0.verified.txt | 139 ++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 139 ++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 140 +++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 140 +++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 54 +++++ ...pe=ObjectId_implementations=2.verified.txt | 54 +++++ ...pe=ObjectId_implementations=4.verified.txt | 55 +++++ ...pe=ObjectId_implementations=6.verified.txt | 55 +++++ ...pe=ObjectId_implementations=0.verified.txt | 97 +++++++++ ...pe=ObjectId_implementations=2.verified.txt | 97 +++++++++ ...pe=ObjectId_implementations=4.verified.txt | 98 +++++++++ ...pe=ObjectId_implementations=6.verified.txt | 98 +++++++++ ...pe=ObjectId_implementations=0.verified.txt | 81 +++++++ ...pe=ObjectId_implementations=2.verified.txt | 81 +++++++ ...pe=ObjectId_implementations=4.verified.txt | 82 ++++++++ ...pe=ObjectId_implementations=6.verified.txt | 82 ++++++++ ...pe=ObjectId_implementations=0.verified.txt | 124 +++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 124 +++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 125 +++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 125 +++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 76 +++++++ ...pe=ObjectId_implementations=2.verified.txt | 76 +++++++ ...pe=ObjectId_implementations=4.verified.txt | 77 +++++++ ...pe=ObjectId_implementations=6.verified.txt | 77 +++++++ ...pe=ObjectId_implementations=0.verified.txt | 119 +++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 119 +++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 120 +++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 120 +++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 103 +++++++++ ...pe=ObjectId_implementations=2.verified.txt | 103 +++++++++ ...pe=ObjectId_implementations=4.verified.txt | 104 +++++++++ ...pe=ObjectId_implementations=6.verified.txt | 104 +++++++++ ...pe=ObjectId_implementations=0.verified.txt | 146 +++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 146 +++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 147 +++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 147 +++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 65 ++++++ ...pe=ObjectId_implementations=2.verified.txt | 65 ++++++ ...pe=ObjectId_implementations=4.verified.txt | 66 ++++++ ...pe=ObjectId_implementations=6.verified.txt | 66 ++++++ ...pe=ObjectId_implementations=0.verified.txt | 63 ++++++ ...pe=ObjectId_implementations=2.verified.txt | 63 ++++++ ...pe=ObjectId_implementations=4.verified.txt | 64 ++++++ ...pe=ObjectId_implementations=6.verified.txt | 64 ++++++ ...pe=ObjectId_implementations=0.verified.txt | 108 ++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 108 ++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 109 ++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 109 ++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 92 ++++++++ ...pe=ObjectId_implementations=2.verified.txt | 92 ++++++++ ...pe=ObjectId_implementations=4.verified.txt | 93 ++++++++ ...pe=ObjectId_implementations=6.verified.txt | 93 ++++++++ ...pe=ObjectId_implementations=0.verified.txt | 135 ++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 135 ++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 136 ++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 136 ++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 87 ++++++++ ...pe=ObjectId_implementations=2.verified.txt | 87 ++++++++ ...pe=ObjectId_implementations=4.verified.txt | 88 ++++++++ ...pe=ObjectId_implementations=6.verified.txt | 88 ++++++++ ...pe=ObjectId_implementations=0.verified.txt | 130 ++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 130 ++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 131 ++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 131 ++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 114 ++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 114 ++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 115 ++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 115 ++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 157 ++++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 157 ++++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 158 ++++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 158 ++++++++++++++ ...ngType=Guid_implementations=0.verified.txt | 49 +++++ ...ngType=Guid_implementations=2.verified.txt | 49 +++++ ...ngType=Guid_implementations=4.verified.txt | 50 +++++ ...ngType=Guid_implementations=6.verified.txt | 50 +++++ ...ingType=Int_implementations=0.verified.txt | 48 +++++ ...ingType=Int_implementations=2.verified.txt | 48 +++++ ...ingType=Int_implementations=4.verified.txt | 49 +++++ ...ingType=Int_implementations=6.verified.txt | 49 +++++ ...ngType=Long_implementations=0.verified.txt | 48 +++++ ...ngType=Long_implementations=2.verified.txt | 48 +++++ ...ngType=Long_implementations=4.verified.txt | 49 +++++ ...ngType=Long_implementations=6.verified.txt | 49 +++++ ...ransitNewId_implementations=0.verified.txt | 49 +++++ ...ransitNewId_implementations=2.verified.txt | 49 +++++ ...ransitNewId_implementations=4.verified.txt | 50 +++++ ...ransitNewId_implementations=6.verified.txt | 50 +++++ ...lableString_implementations=0.verified.txt | 64 ++++++ ...lableString_implementations=2.verified.txt | 64 ++++++ ...lableString_implementations=4.verified.txt | 74 +++++++ ...lableString_implementations=6.verified.txt | 74 +++++++ ...pe=ObjectId_implementations=0.verified.txt | 64 ++++++ ...pe=ObjectId_implementations=2.verified.txt | 64 ++++++ ...pe=ObjectId_implementations=4.verified.txt | 65 ++++++ ...pe=ObjectId_implementations=6.verified.txt | 65 ++++++ ...Type=String_implementations=0.verified.txt | 57 +++++ ...Type=String_implementations=2.verified.txt | 57 +++++ ...Type=String_implementations=4.verified.txt | 67 ++++++ ...Type=String_implementations=6.verified.txt | 67 ++++++ ...ngType=Guid_implementations=0.verified.txt | 91 ++++++++ ...ngType=Guid_implementations=2.verified.txt | 91 ++++++++ ...ngType=Guid_implementations=4.verified.txt | 92 ++++++++ ...ngType=Guid_implementations=6.verified.txt | 92 ++++++++ ...ingType=Int_implementations=0.verified.txt | 90 ++++++++ ...ingType=Int_implementations=2.verified.txt | 90 ++++++++ ...ingType=Int_implementations=4.verified.txt | 91 ++++++++ ...ingType=Int_implementations=6.verified.txt | 91 ++++++++ ...ngType=Long_implementations=0.verified.txt | 92 ++++++++ ...ngType=Long_implementations=2.verified.txt | 92 ++++++++ ...ngType=Long_implementations=4.verified.txt | 93 ++++++++ ...ngType=Long_implementations=6.verified.txt | 93 ++++++++ ...ransitNewId_implementations=0.verified.txt | 99 +++++++++ ...ransitNewId_implementations=2.verified.txt | 99 +++++++++ ...ransitNewId_implementations=4.verified.txt | 100 +++++++++ ...ransitNewId_implementations=6.verified.txt | 100 +++++++++ ...lableString_implementations=0.verified.txt | 107 ++++++++++ ...lableString_implementations=2.verified.txt | 107 ++++++++++ ...lableString_implementations=4.verified.txt | 117 +++++++++++ ...lableString_implementations=6.verified.txt | 117 +++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 107 ++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 107 ++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 108 ++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 108 ++++++++++ ...Type=String_implementations=0.verified.txt | 95 +++++++++ ...Type=String_implementations=2.verified.txt | 95 +++++++++ ...Type=String_implementations=4.verified.txt | 105 ++++++++++ ...Type=String_implementations=6.verified.txt | 105 ++++++++++ ...ngType=Guid_implementations=0.verified.txt | 70 +++++++ ...ngType=Guid_implementations=2.verified.txt | 70 +++++++ ...ngType=Guid_implementations=4.verified.txt | 71 +++++++ ...ngType=Guid_implementations=6.verified.txt | 71 +++++++ ...ingType=Int_implementations=0.verified.txt | 69 ++++++ ...ingType=Int_implementations=2.verified.txt | 69 ++++++ ...ingType=Int_implementations=4.verified.txt | 70 +++++++ ...ingType=Int_implementations=6.verified.txt | 70 +++++++ ...ngType=Long_implementations=0.verified.txt | 69 ++++++ ...ngType=Long_implementations=2.verified.txt | 69 ++++++ ...ngType=Long_implementations=4.verified.txt | 70 +++++++ ...ngType=Long_implementations=6.verified.txt | 70 +++++++ ...ransitNewId_implementations=0.verified.txt | 70 +++++++ ...ransitNewId_implementations=2.verified.txt | 70 +++++++ ...ransitNewId_implementations=4.verified.txt | 71 +++++++ ...ransitNewId_implementations=6.verified.txt | 71 +++++++ ...lableString_implementations=0.verified.txt | 91 ++++++++ ...lableString_implementations=2.verified.txt | 91 ++++++++ ...lableString_implementations=4.verified.txt | 101 +++++++++ ...lableString_implementations=6.verified.txt | 101 +++++++++ ...pe=ObjectId_implementations=0.verified.txt | 91 ++++++++ ...pe=ObjectId_implementations=2.verified.txt | 91 ++++++++ ...pe=ObjectId_implementations=4.verified.txt | 92 ++++++++ ...pe=ObjectId_implementations=6.verified.txt | 92 ++++++++ ...Type=String_implementations=0.verified.txt | 77 +++++++ ...Type=String_implementations=2.verified.txt | 77 +++++++ ...Type=String_implementations=4.verified.txt | 87 ++++++++ ...Type=String_implementations=6.verified.txt | 87 ++++++++ ...pe=ObjectId_implementations=0.verified.txt | 106 ++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 106 ++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 107 ++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 107 ++++++++++ ...ngType=Guid_implementations=0.verified.txt | 112 ++++++++++ ...ngType=Guid_implementations=2.verified.txt | 112 ++++++++++ ...ngType=Guid_implementations=4.verified.txt | 113 ++++++++++ ...ngType=Guid_implementations=6.verified.txt | 113 ++++++++++ ...ingType=Int_implementations=0.verified.txt | 111 ++++++++++ ...ingType=Int_implementations=2.verified.txt | 111 ++++++++++ ...ingType=Int_implementations=4.verified.txt | 112 ++++++++++ ...ingType=Int_implementations=6.verified.txt | 112 ++++++++++ ...ngType=Long_implementations=0.verified.txt | 113 ++++++++++ ...ngType=Long_implementations=2.verified.txt | 113 ++++++++++ ...ngType=Long_implementations=4.verified.txt | 114 ++++++++++ ...ngType=Long_implementations=6.verified.txt | 114 ++++++++++ ...ransitNewId_implementations=0.verified.txt | 120 +++++++++++ ...ransitNewId_implementations=2.verified.txt | 120 +++++++++++ ...ransitNewId_implementations=4.verified.txt | 121 +++++++++++ ...ransitNewId_implementations=6.verified.txt | 121 +++++++++++ ...lableString_implementations=0.verified.txt | 134 ++++++++++++ ...lableString_implementations=2.verified.txt | 134 ++++++++++++ ...lableString_implementations=4.verified.txt | 144 +++++++++++++ ...lableString_implementations=6.verified.txt | 144 +++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 134 ++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 134 ++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 135 ++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 135 ++++++++++++ ...Type=String_implementations=0.verified.txt | 115 ++++++++++ ...Type=String_implementations=2.verified.txt | 115 ++++++++++ ...Type=String_implementations=4.verified.txt | 125 +++++++++++ ...Type=String_implementations=6.verified.txt | 125 +++++++++++ ...ngType=Guid_implementations=0.verified.txt | 63 ++++++ ...ngType=Guid_implementations=2.verified.txt | 63 ++++++ ...ngType=Guid_implementations=4.verified.txt | 64 ++++++ ...ngType=Guid_implementations=6.verified.txt | 64 ++++++ ...ingType=Int_implementations=0.verified.txt | 62 ++++++ ...ingType=Int_implementations=2.verified.txt | 62 ++++++ ...ingType=Int_implementations=4.verified.txt | 63 ++++++ ...ingType=Int_implementations=6.verified.txt | 63 ++++++ ...ngType=Long_implementations=0.verified.txt | 62 ++++++ ...ngType=Long_implementations=2.verified.txt | 62 ++++++ ...ngType=Long_implementations=4.verified.txt | 63 ++++++ ...ngType=Long_implementations=6.verified.txt | 63 ++++++ ...ransitNewId_implementations=0.verified.txt | 63 ++++++ ...ransitNewId_implementations=2.verified.txt | 63 ++++++ ...ransitNewId_implementations=4.verified.txt | 64 ++++++ ...ransitNewId_implementations=6.verified.txt | 64 ++++++ ...lableString_implementations=0.verified.txt | 85 ++++++++ ...lableString_implementations=2.verified.txt | 85 ++++++++ ...lableString_implementations=4.verified.txt | 95 +++++++++ ...lableString_implementations=6.verified.txt | 95 +++++++++ ...pe=ObjectId_implementations=0.verified.txt | 86 ++++++++ ...pe=ObjectId_implementations=2.verified.txt | 86 ++++++++ ...pe=ObjectId_implementations=4.verified.txt | 87 ++++++++ ...pe=ObjectId_implementations=6.verified.txt | 87 ++++++++ ...Type=String_implementations=0.verified.txt | 71 +++++++ ...Type=String_implementations=2.verified.txt | 71 +++++++ ...Type=String_implementations=4.verified.txt | 81 +++++++ ...Type=String_implementations=6.verified.txt | 81 +++++++ ...ngType=Guid_implementations=0.verified.txt | 105 ++++++++++ ...ngType=Guid_implementations=2.verified.txt | 105 ++++++++++ ...ngType=Guid_implementations=4.verified.txt | 106 ++++++++++ ...ngType=Guid_implementations=6.verified.txt | 106 ++++++++++ ...ingType=Int_implementations=0.verified.txt | 104 +++++++++ ...ingType=Int_implementations=2.verified.txt | 104 +++++++++ ...ingType=Int_implementations=4.verified.txt | 105 ++++++++++ ...ingType=Int_implementations=6.verified.txt | 105 ++++++++++ ...ngType=Long_implementations=0.verified.txt | 106 ++++++++++ ...ngType=Long_implementations=2.verified.txt | 106 ++++++++++ ...ngType=Long_implementations=4.verified.txt | 107 ++++++++++ ...ngType=Long_implementations=6.verified.txt | 107 ++++++++++ ...ransitNewId_implementations=0.verified.txt | 113 ++++++++++ ...ransitNewId_implementations=2.verified.txt | 113 ++++++++++ ...ransitNewId_implementations=4.verified.txt | 114 ++++++++++ ...ransitNewId_implementations=6.verified.txt | 114 ++++++++++ ...lableString_implementations=0.verified.txt | 128 +++++++++++ ...lableString_implementations=2.verified.txt | 128 +++++++++++ ...lableString_implementations=4.verified.txt | 138 ++++++++++++ ...lableString_implementations=6.verified.txt | 138 ++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 129 ++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 129 ++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 130 ++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 130 ++++++++++++ ...Type=String_implementations=0.verified.txt | 109 ++++++++++ ...Type=String_implementations=2.verified.txt | 109 ++++++++++ ...Type=String_implementations=4.verified.txt | 119 +++++++++++ ...Type=String_implementations=6.verified.txt | 119 +++++++++++ ...ngType=Guid_implementations=0.verified.txt | 84 ++++++++ ...ngType=Guid_implementations=2.verified.txt | 84 ++++++++ ...ngType=Guid_implementations=4.verified.txt | 85 ++++++++ ...ngType=Guid_implementations=6.verified.txt | 85 ++++++++ ...ingType=Int_implementations=0.verified.txt | 83 ++++++++ ...ingType=Int_implementations=2.verified.txt | 83 ++++++++ ...ingType=Int_implementations=4.verified.txt | 84 ++++++++ ...ingType=Int_implementations=6.verified.txt | 84 ++++++++ ...ngType=Long_implementations=0.verified.txt | 83 ++++++++ ...ngType=Long_implementations=2.verified.txt | 83 ++++++++ ...ngType=Long_implementations=4.verified.txt | 84 ++++++++ ...ngType=Long_implementations=6.verified.txt | 84 ++++++++ ...ransitNewId_implementations=0.verified.txt | 84 ++++++++ ...ransitNewId_implementations=2.verified.txt | 84 ++++++++ ...ransitNewId_implementations=4.verified.txt | 85 ++++++++ ...ransitNewId_implementations=6.verified.txt | 85 ++++++++ ...lableString_implementations=0.verified.txt | 112 ++++++++++ ...lableString_implementations=2.verified.txt | 112 ++++++++++ ...lableString_implementations=4.verified.txt | 122 +++++++++++ ...lableString_implementations=6.verified.txt | 122 +++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 113 ++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 113 ++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 114 ++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 114 ++++++++++ ...Type=String_implementations=0.verified.txt | 91 ++++++++ ...Type=String_implementations=2.verified.txt | 91 ++++++++ ...Type=String_implementations=4.verified.txt | 101 +++++++++ ...Type=String_implementations=6.verified.txt | 101 +++++++++ ...ngType=Guid_implementations=0.verified.txt | 126 +++++++++++ ...ngType=Guid_implementations=2.verified.txt | 126 +++++++++++ ...ngType=Guid_implementations=4.verified.txt | 127 +++++++++++ ...ngType=Guid_implementations=6.verified.txt | 127 +++++++++++ ...ingType=Int_implementations=0.verified.txt | 125 +++++++++++ ...ingType=Int_implementations=2.verified.txt | 125 +++++++++++ ...ingType=Int_implementations=4.verified.txt | 126 +++++++++++ ...ingType=Int_implementations=6.verified.txt | 126 +++++++++++ ...ngType=Long_implementations=0.verified.txt | 127 +++++++++++ ...ngType=Long_implementations=2.verified.txt | 127 +++++++++++ ...ngType=Long_implementations=4.verified.txt | 128 +++++++++++ ...ngType=Long_implementations=6.verified.txt | 128 +++++++++++ ...ransitNewId_implementations=0.verified.txt | 134 ++++++++++++ ...ransitNewId_implementations=2.verified.txt | 134 ++++++++++++ ...ransitNewId_implementations=4.verified.txt | 135 ++++++++++++ ...ransitNewId_implementations=6.verified.txt | 135 ++++++++++++ ...lableString_implementations=0.verified.txt | 155 ++++++++++++++ ...lableString_implementations=2.verified.txt | 155 ++++++++++++++ ...lableString_implementations=4.verified.txt | 165 +++++++++++++++ ...lableString_implementations=6.verified.txt | 165 +++++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 156 ++++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 156 ++++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 157 ++++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 157 ++++++++++++++ ...Type=String_implementations=0.verified.txt | 129 ++++++++++++ ...Type=String_implementations=2.verified.txt | 129 ++++++++++++ ...Type=String_implementations=4.verified.txt | 139 ++++++++++++ ...Type=String_implementations=6.verified.txt | 139 ++++++++++++ ...ngType=Guid_implementations=0.verified.txt | 60 ++++++ ...ngType=Guid_implementations=2.verified.txt | 60 ++++++ ...ngType=Guid_implementations=4.verified.txt | 61 ++++++ ...ngType=Guid_implementations=6.verified.txt | 61 ++++++ ...ingType=Int_implementations=0.verified.txt | 59 ++++++ ...ingType=Int_implementations=2.verified.txt | 59 ++++++ ...ingType=Int_implementations=4.verified.txt | 60 ++++++ ...ingType=Int_implementations=6.verified.txt | 60 ++++++ ...ngType=Long_implementations=0.verified.txt | 59 ++++++ ...ngType=Long_implementations=2.verified.txt | 59 ++++++ ...ngType=Long_implementations=4.verified.txt | 60 ++++++ ...ngType=Long_implementations=6.verified.txt | 60 ++++++ ...ransitNewId_implementations=0.verified.txt | 60 ++++++ ...ransitNewId_implementations=2.verified.txt | 60 ++++++ ...ransitNewId_implementations=4.verified.txt | 61 ++++++ ...ransitNewId_implementations=6.verified.txt | 61 ++++++ ...lableString_implementations=0.verified.txt | 75 +++++++ ...lableString_implementations=2.verified.txt | 75 +++++++ ...lableString_implementations=4.verified.txt | 85 ++++++++ ...lableString_implementations=6.verified.txt | 85 ++++++++ ...pe=ObjectId_implementations=0.verified.txt | 75 +++++++ ...pe=ObjectId_implementations=2.verified.txt | 75 +++++++ ...pe=ObjectId_implementations=4.verified.txt | 76 +++++++ ...pe=ObjectId_implementations=6.verified.txt | 76 +++++++ ...Type=String_implementations=0.verified.txt | 68 ++++++ ...Type=String_implementations=2.verified.txt | 68 ++++++ ...Type=String_implementations=4.verified.txt | 78 +++++++ ...Type=String_implementations=6.verified.txt | 78 +++++++ ...ngType=Guid_implementations=0.verified.txt | 102 +++++++++ ...ngType=Guid_implementations=2.verified.txt | 102 +++++++++ ...ngType=Guid_implementations=4.verified.txt | 103 +++++++++ ...ngType=Guid_implementations=6.verified.txt | 103 +++++++++ ...ingType=Int_implementations=0.verified.txt | 101 +++++++++ ...ingType=Int_implementations=2.verified.txt | 101 +++++++++ ...ingType=Int_implementations=4.verified.txt | 102 +++++++++ ...ingType=Int_implementations=6.verified.txt | 102 +++++++++ ...ngType=Long_implementations=0.verified.txt | 103 +++++++++ ...ngType=Long_implementations=2.verified.txt | 103 +++++++++ ...ngType=Long_implementations=4.verified.txt | 104 +++++++++ ...ngType=Long_implementations=6.verified.txt | 104 +++++++++ ...ransitNewId_implementations=0.verified.txt | 110 ++++++++++ ...ransitNewId_implementations=2.verified.txt | 110 ++++++++++ ...ransitNewId_implementations=4.verified.txt | 111 ++++++++++ ...ransitNewId_implementations=6.verified.txt | 111 ++++++++++ ...lableString_implementations=0.verified.txt | 118 +++++++++++ ...lableString_implementations=2.verified.txt | 118 +++++++++++ ...lableString_implementations=4.verified.txt | 128 +++++++++++ ...lableString_implementations=6.verified.txt | 128 +++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 118 +++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 118 +++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 119 +++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 119 +++++++++++ ...Type=String_implementations=0.verified.txt | 106 ++++++++++ ...Type=String_implementations=2.verified.txt | 106 ++++++++++ ...Type=String_implementations=4.verified.txt | 116 ++++++++++ ...Type=String_implementations=6.verified.txt | 116 ++++++++++ ...ngType=Guid_implementations=0.verified.txt | 81 +++++++ ...ngType=Guid_implementations=2.verified.txt | 81 +++++++ ...ngType=Guid_implementations=4.verified.txt | 82 ++++++++ ...ngType=Guid_implementations=6.verified.txt | 82 ++++++++ ...ingType=Int_implementations=0.verified.txt | 80 +++++++ ...ingType=Int_implementations=2.verified.txt | 80 +++++++ ...ingType=Int_implementations=4.verified.txt | 81 +++++++ ...ingType=Int_implementations=6.verified.txt | 81 +++++++ ...ngType=Long_implementations=0.verified.txt | 80 +++++++ ...ngType=Long_implementations=2.verified.txt | 80 +++++++ ...ngType=Long_implementations=4.verified.txt | 81 +++++++ ...ngType=Long_implementations=6.verified.txt | 81 +++++++ ...ransitNewId_implementations=0.verified.txt | 81 +++++++ ...ransitNewId_implementations=2.verified.txt | 81 +++++++ ...ransitNewId_implementations=4.verified.txt | 82 ++++++++ ...ransitNewId_implementations=6.verified.txt | 82 ++++++++ ...lableString_implementations=0.verified.txt | 102 +++++++++ ...lableString_implementations=2.verified.txt | 102 +++++++++ ...lableString_implementations=4.verified.txt | 112 ++++++++++ ...lableString_implementations=6.verified.txt | 112 ++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 102 +++++++++ ...pe=ObjectId_implementations=2.verified.txt | 102 +++++++++ ...pe=ObjectId_implementations=4.verified.txt | 103 +++++++++ ...pe=ObjectId_implementations=6.verified.txt | 103 +++++++++ ...Type=String_implementations=0.verified.txt | 88 ++++++++ ...Type=String_implementations=2.verified.txt | 88 ++++++++ ...Type=String_implementations=4.verified.txt | 98 +++++++++ ...Type=String_implementations=6.verified.txt | 98 +++++++++ ...ngType=Guid_implementations=0.verified.txt | 123 +++++++++++ ...ngType=Guid_implementations=2.verified.txt | 123 +++++++++++ ...ngType=Guid_implementations=4.verified.txt | 124 +++++++++++ ...ngType=Guid_implementations=6.verified.txt | 124 +++++++++++ ...ingType=Int_implementations=0.verified.txt | 122 +++++++++++ ...ingType=Int_implementations=2.verified.txt | 122 +++++++++++ ...ingType=Int_implementations=4.verified.txt | 123 +++++++++++ ...ingType=Int_implementations=6.verified.txt | 123 +++++++++++ ...ngType=Long_implementations=0.verified.txt | 124 +++++++++++ ...ngType=Long_implementations=2.verified.txt | 124 +++++++++++ ...ngType=Long_implementations=4.verified.txt | 125 +++++++++++ ...ngType=Long_implementations=6.verified.txt | 125 +++++++++++ ...ransitNewId_implementations=0.verified.txt | 131 ++++++++++++ ...ransitNewId_implementations=2.verified.txt | 131 ++++++++++++ ...ransitNewId_implementations=4.verified.txt | 132 ++++++++++++ ...ransitNewId_implementations=6.verified.txt | 132 ++++++++++++ ...lableString_implementations=0.verified.txt | 145 +++++++++++++ ...lableString_implementations=2.verified.txt | 145 +++++++++++++ ...lableString_implementations=4.verified.txt | 155 ++++++++++++++ ...lableString_implementations=6.verified.txt | 155 ++++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 145 +++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 145 +++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 146 +++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 146 +++++++++++++ ...Type=String_implementations=0.verified.txt | 126 +++++++++++ ...Type=String_implementations=2.verified.txt | 126 +++++++++++ ...Type=String_implementations=4.verified.txt | 136 ++++++++++++ ...Type=String_implementations=6.verified.txt | 136 ++++++++++++ ...ngType=Guid_implementations=0.verified.txt | 74 +++++++ ...ngType=Guid_implementations=2.verified.txt | 74 +++++++ ...ngType=Guid_implementations=4.verified.txt | 75 +++++++ ...ngType=Guid_implementations=6.verified.txt | 75 +++++++ ...ingType=Int_implementations=0.verified.txt | 73 +++++++ ...ingType=Int_implementations=2.verified.txt | 73 +++++++ ...ingType=Int_implementations=4.verified.txt | 74 +++++++ ...ingType=Int_implementations=6.verified.txt | 74 +++++++ ...ngType=Long_implementations=0.verified.txt | 73 +++++++ ...ngType=Long_implementations=2.verified.txt | 73 +++++++ ...ngType=Long_implementations=4.verified.txt | 74 +++++++ ...ngType=Long_implementations=6.verified.txt | 74 +++++++ ...ransitNewId_implementations=0.verified.txt | 74 +++++++ ...ransitNewId_implementations=2.verified.txt | 74 +++++++ ...ransitNewId_implementations=4.verified.txt | 75 +++++++ ...ransitNewId_implementations=6.verified.txt | 75 +++++++ ...lableString_implementations=0.verified.txt | 96 +++++++++ ...lableString_implementations=2.verified.txt | 96 +++++++++ ...lableString_implementations=4.verified.txt | 106 ++++++++++ ...lableString_implementations=6.verified.txt | 106 ++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 97 +++++++++ ...pe=ObjectId_implementations=2.verified.txt | 97 +++++++++ ...pe=ObjectId_implementations=4.verified.txt | 98 +++++++++ ...pe=ObjectId_implementations=6.verified.txt | 98 +++++++++ ...Type=String_implementations=0.verified.txt | 82 ++++++++ ...Type=String_implementations=2.verified.txt | 82 ++++++++ ...Type=String_implementations=4.verified.txt | 92 ++++++++ ...Type=String_implementations=6.verified.txt | 92 ++++++++ ...pe=ObjectId_implementations=0.verified.txt | 58 +++++ ...pe=ObjectId_implementations=2.verified.txt | 58 +++++ ...pe=ObjectId_implementations=4.verified.txt | 59 ++++++ ...pe=ObjectId_implementations=6.verified.txt | 59 ++++++ ...ngType=Guid_implementations=0.verified.txt | 116 ++++++++++ ...ngType=Guid_implementations=2.verified.txt | 116 ++++++++++ ...ngType=Guid_implementations=4.verified.txt | 117 +++++++++++ ...ngType=Guid_implementations=6.verified.txt | 117 +++++++++++ ...ingType=Int_implementations=0.verified.txt | 115 ++++++++++ ...ingType=Int_implementations=2.verified.txt | 115 ++++++++++ ...ingType=Int_implementations=4.verified.txt | 116 ++++++++++ ...ingType=Int_implementations=6.verified.txt | 116 ++++++++++ ...ngType=Long_implementations=0.verified.txt | 117 +++++++++++ ...ngType=Long_implementations=2.verified.txt | 117 +++++++++++ ...ngType=Long_implementations=4.verified.txt | 118 +++++++++++ ...ngType=Long_implementations=6.verified.txt | 118 +++++++++++ ...ransitNewId_implementations=0.verified.txt | 124 +++++++++++ ...ransitNewId_implementations=2.verified.txt | 124 +++++++++++ ...ransitNewId_implementations=4.verified.txt | 125 +++++++++++ ...ransitNewId_implementations=6.verified.txt | 125 +++++++++++ ...lableString_implementations=0.verified.txt | 139 ++++++++++++ ...lableString_implementations=2.verified.txt | 139 ++++++++++++ ...lableString_implementations=4.verified.txt | 149 +++++++++++++ ...lableString_implementations=6.verified.txt | 149 +++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 140 +++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 140 +++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 141 +++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 141 +++++++++++++ ...Type=String_implementations=0.verified.txt | 120 +++++++++++ ...Type=String_implementations=2.verified.txt | 120 +++++++++++ ...Type=String_implementations=4.verified.txt | 130 ++++++++++++ ...Type=String_implementations=6.verified.txt | 130 ++++++++++++ ...ngType=Guid_implementations=0.verified.txt | 95 +++++++++ ...ngType=Guid_implementations=2.verified.txt | 95 +++++++++ ...ngType=Guid_implementations=4.verified.txt | 96 +++++++++ ...ngType=Guid_implementations=6.verified.txt | 96 +++++++++ ...ingType=Int_implementations=0.verified.txt | 94 +++++++++ ...ingType=Int_implementations=2.verified.txt | 94 +++++++++ ...ingType=Int_implementations=4.verified.txt | 95 +++++++++ ...ingType=Int_implementations=6.verified.txt | 95 +++++++++ ...ngType=Long_implementations=0.verified.txt | 94 +++++++++ ...ngType=Long_implementations=2.verified.txt | 94 +++++++++ ...ngType=Long_implementations=4.verified.txt | 95 +++++++++ ...ngType=Long_implementations=6.verified.txt | 95 +++++++++ ...ransitNewId_implementations=0.verified.txt | 95 +++++++++ ...ransitNewId_implementations=2.verified.txt | 95 +++++++++ ...ransitNewId_implementations=4.verified.txt | 96 +++++++++ ...ransitNewId_implementations=6.verified.txt | 96 +++++++++ ...lableString_implementations=0.verified.txt | 123 +++++++++++ ...lableString_implementations=2.verified.txt | 123 +++++++++++ ...lableString_implementations=4.verified.txt | 133 ++++++++++++ ...lableString_implementations=6.verified.txt | 133 ++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 124 +++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 124 +++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 125 +++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 125 +++++++++++ ...Type=String_implementations=0.verified.txt | 102 +++++++++ ...Type=String_implementations=2.verified.txt | 102 +++++++++ ...Type=String_implementations=4.verified.txt | 112 ++++++++++ ...Type=String_implementations=6.verified.txt | 112 ++++++++++ ...ngType=Guid_implementations=0.verified.txt | 137 ++++++++++++ ...ngType=Guid_implementations=2.verified.txt | 137 ++++++++++++ ...ngType=Guid_implementations=4.verified.txt | 138 ++++++++++++ ...ngType=Guid_implementations=6.verified.txt | 138 ++++++++++++ ...ingType=Int_implementations=0.verified.txt | 136 ++++++++++++ ...ingType=Int_implementations=2.verified.txt | 136 ++++++++++++ ...ingType=Int_implementations=4.verified.txt | 137 ++++++++++++ ...ingType=Int_implementations=6.verified.txt | 137 ++++++++++++ ...ngType=Long_implementations=0.verified.txt | 138 ++++++++++++ ...ngType=Long_implementations=2.verified.txt | 138 ++++++++++++ ...ngType=Long_implementations=4.verified.txt | 139 ++++++++++++ ...ngType=Long_implementations=6.verified.txt | 139 ++++++++++++ ...ransitNewId_implementations=0.verified.txt | 145 +++++++++++++ ...ransitNewId_implementations=2.verified.txt | 145 +++++++++++++ ...ransitNewId_implementations=4.verified.txt | 146 +++++++++++++ ...ransitNewId_implementations=6.verified.txt | 146 +++++++++++++ ...lableString_implementations=0.verified.txt | 166 +++++++++++++++ ...lableString_implementations=2.verified.txt | 166 +++++++++++++++ ...lableString_implementations=4.verified.txt | 176 ++++++++++++++++ ...lableString_implementations=6.verified.txt | 176 ++++++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 167 +++++++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 167 +++++++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 168 +++++++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 168 +++++++++++++++ ...Type=String_implementations=0.verified.txt | 140 +++++++++++++ ...Type=String_implementations=2.verified.txt | 140 +++++++++++++ ...Type=String_implementations=4.verified.txt | 150 +++++++++++++ ...Type=String_implementations=6.verified.txt | 150 +++++++++++++ ...ngType=Guid_implementations=0.verified.txt | 67 ++++++ ...ngType=Guid_implementations=2.verified.txt | 67 ++++++ ...ngType=Guid_implementations=4.verified.txt | 68 ++++++ ...ngType=Guid_implementations=6.verified.txt | 68 ++++++ ...ingType=Int_implementations=0.verified.txt | 67 ++++++ ...ingType=Int_implementations=2.verified.txt | 67 ++++++ ...ingType=Int_implementations=4.verified.txt | 68 ++++++ ...ingType=Int_implementations=6.verified.txt | 68 ++++++ ...ngType=Long_implementations=0.verified.txt | 68 ++++++ ...ngType=Long_implementations=2.verified.txt | 68 ++++++ ...ngType=Long_implementations=4.verified.txt | 69 ++++++ ...ngType=Long_implementations=6.verified.txt | 69 ++++++ ...ransitNewId_implementations=0.verified.txt | 67 ++++++ ...ransitNewId_implementations=2.verified.txt | 67 ++++++ ...ransitNewId_implementations=4.verified.txt | 68 ++++++ ...ransitNewId_implementations=6.verified.txt | 68 ++++++ ...lableString_implementations=0.verified.txt | 83 ++++++++ ...lableString_implementations=2.verified.txt | 83 ++++++++ ...lableString_implementations=4.verified.txt | 93 ++++++++ ...lableString_implementations=6.verified.txt | 93 ++++++++ ...pe=ObjectId_implementations=0.verified.txt | 82 ++++++++ ...pe=ObjectId_implementations=2.verified.txt | 82 ++++++++ ...pe=ObjectId_implementations=4.verified.txt | 83 ++++++++ ...pe=ObjectId_implementations=6.verified.txt | 83 ++++++++ ...Type=String_implementations=0.verified.txt | 74 +++++++ ...Type=String_implementations=2.verified.txt | 74 +++++++ ...Type=String_implementations=4.verified.txt | 84 ++++++++ ...Type=String_implementations=6.verified.txt | 84 ++++++++ ...ngType=Guid_implementations=0.verified.txt | 109 ++++++++++ ...ngType=Guid_implementations=2.verified.txt | 109 ++++++++++ ...ngType=Guid_implementations=4.verified.txt | 110 ++++++++++ ...ngType=Guid_implementations=6.verified.txt | 110 ++++++++++ ...ingType=Int_implementations=0.verified.txt | 109 ++++++++++ ...ingType=Int_implementations=2.verified.txt | 109 ++++++++++ ...ingType=Int_implementations=4.verified.txt | 110 ++++++++++ ...ingType=Int_implementations=6.verified.txt | 110 ++++++++++ ...ngType=Long_implementations=0.verified.txt | 112 ++++++++++ ...ngType=Long_implementations=2.verified.txt | 112 ++++++++++ ...ngType=Long_implementations=4.verified.txt | 113 ++++++++++ ...ngType=Long_implementations=6.verified.txt | 113 ++++++++++ ...ransitNewId_implementations=0.verified.txt | 117 +++++++++++ ...ransitNewId_implementations=2.verified.txt | 117 +++++++++++ ...ransitNewId_implementations=4.verified.txt | 118 +++++++++++ ...ransitNewId_implementations=6.verified.txt | 118 +++++++++++ ...lableString_implementations=0.verified.txt | 126 +++++++++++ ...lableString_implementations=2.verified.txt | 126 +++++++++++ ...lableString_implementations=4.verified.txt | 136 ++++++++++++ ...lableString_implementations=6.verified.txt | 136 ++++++++++++ ...pe=ObjectId_implementations=0.verified.txt | 125 +++++++++++ ...pe=ObjectId_implementations=2.verified.txt | 125 +++++++++++ ...pe=ObjectId_implementations=4.verified.txt | 126 +++++++++++ ...pe=ObjectId_implementations=6.verified.txt | 126 +++++++++++ ...Type=String_implementations=0.verified.txt | 112 ++++++++++ ...Type=String_implementations=2.verified.txt | 112 ++++++++++ ...Type=String_implementations=4.verified.txt | 122 +++++++++++ ...Type=String_implementations=6.verified.txt | 122 +++++++++++ 2051 files changed, 227848 insertions(+), 1 deletion(-) create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=String_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Guid_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Guid_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Guid_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Guid_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Int_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Int_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Int_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Int_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Long_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Long_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Long_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Long_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=MassTransitNewId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=MassTransitNewId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=MassTransitNewId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=MassTransitNewId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=NullableString_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=NullableString_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=NullableString_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=NullableString_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=6.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=String_implementations=0.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=String_implementations=2.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=String_implementations=4.verified.txt create mode 100644 test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=String_implementations=6.verified.txt diff --git a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_Base.cs b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_Base.cs index b56cf963d..fc664a6e4 100644 --- a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_Base.cs +++ b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_Base.cs @@ -21,4 +21,5 @@ public override bool Equals(object obj) public override string ToString() => Value.ToString(); public static bool operator ==(TESTID a, TESTID b) => a.Equals(b); - public static bool operator !=(TESTID a, TESTID b) => !(a == b); \ No newline at end of file + public static bool operator !=(TESTID a, TESTID b) => !(a == b); + \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.verified.txt index 4ea1be319..74d859696 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdBackingType.verified.txt @@ -31,6 +31,7 @@ namespace StronglyTypedIds Long = 4, NullableString = 5, MassTransitNewId = 6, + ObjectId = 7 } } #endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.verified.txt index 32b324616..477021e78 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/EmbeddedResourceTests.EmittedResourceIsSameAsCompiledResource_resource=StronglyTypedIdConverter.verified.txt @@ -59,6 +59,11 @@ namespace StronglyTypedIds /// Creates a Dapper TypeHandler for converting to and from the type /// DapperTypeHandler = 32, + + /// + /// Creates a Mongo Serializer for converting string to and from type + /// + MongoSerializer = 64, } } #endif \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..1b266fc0d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,39 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..f45358787 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,39 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..add723dbc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=4.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.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..584b84a69 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=6.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.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..bf245b783 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=0.verified.txt @@ -0,0 +1,91 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..200cfb6bf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=2.verified.txt @@ -0,0 +1,91 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..1d2119e12 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=4.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..be66c0ab1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Guid_implementations=6.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..1b3a443af --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=0.verified.txt @@ -0,0 +1,91 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..440a73743 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=2.verified.txt @@ -0,0 +1,91 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..ed5311bc2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=4.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..6a7c5c57a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Int_implementations=6.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..ba1c05dc3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=0.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..19e7fd59f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=2.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..dd4873707 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=4.verified.txt @@ -0,0 +1,93 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..7da8d8333 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=Long_implementations=6.verified.txt @@ -0,0 +1,93 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..9e72afdb0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=0.verified.txt @@ -0,0 +1,91 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..ac772db15 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=2.verified.txt @@ -0,0 +1,91 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..49b36c5c8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=4.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..9f587d8d8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=MassTransitNewId_implementations=6.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..bdf1ff7c5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=0.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..c5d7c0307 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=2.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..0445ae543 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=4.verified.txt @@ -0,0 +1,123 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..13f601e2a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=NullableString_implementations=6.verified.txt @@ -0,0 +1,123 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..1cbdf83e0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,112 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..c255b2036 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,112 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..8c52540e4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..f243643cb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..ce53fb9fb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..8507545d4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..5cbc7fdfe --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=4.verified.txt @@ -0,0 +1,107 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..641cb0e9c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=String_implementations=6.verified.txt @@ -0,0 +1,107 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..76ad56067 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=0.verified.txt @@ -0,0 +1,133 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..ec8a6cec1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=2.verified.txt @@ -0,0 +1,133 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..c88a7186e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=4.verified.txt @@ -0,0 +1,134 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..4be3c5656 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Guid_implementations=6.verified.txt @@ -0,0 +1,134 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..ead99ce84 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=0.verified.txt @@ -0,0 +1,133 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..27a1a1319 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=2.verified.txt @@ -0,0 +1,133 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..f3dcdcc67 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=4.verified.txt @@ -0,0 +1,134 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..40bf927e5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Int_implementations=6.verified.txt @@ -0,0 +1,134 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..20afff557 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=0.verified.txt @@ -0,0 +1,136 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..62bed1ede --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=2.verified.txt @@ -0,0 +1,136 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..07ee8229d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=4.verified.txt @@ -0,0 +1,137 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..4e0735288 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=Long_implementations=6.verified.txt @@ -0,0 +1,137 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..acb2feac3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..2dbfb3c91 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..c6e213324 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..4c8c78790 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=MassTransitNewId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..c14db4911 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=0.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 +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..aba992e7c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=2.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 +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..1ad44ca71 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=4.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 +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..bbd582874 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=NullableString_implementations=6.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 +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..0ad4e0dea --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=0.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..8cf3f12d4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=2.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..b1d594d43 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=4.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..e9dfc89ff --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=6.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..f455d513b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=String_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..94ff618cd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=String_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..2b0c4156e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=String_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..ee55106fb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=String_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..ec88d9597 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Guid_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..e98591766 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Guid_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..998451720 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Guid_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..f998ac34f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Guid_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..e69f7a183 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Int_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..5147f9bd4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Int_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..2082f38d6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..2f4bcf986 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..fcdc1ee9f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Long_implementations=0.verified.txt @@ -0,0 +1,85 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..c6c726783 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Long_implementations=2.verified.txt @@ -0,0 +1,85 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..141a1e279 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Long_implementations=4.verified.txt @@ -0,0 +1,86 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..802cf6cc9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=Long_implementations=6.verified.txt @@ -0,0 +1,86 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..9fa0b9611 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=MassTransitNewId_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..07e388c4d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=MassTransitNewId_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..23010b20b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=MassTransitNewId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..405dc3cd1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=MassTransitNewId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..14b81d158 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=NullableString_implementations=0.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..3cb0fccb8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=NullableString_implementations=2.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..79490fd16 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=NullableString_implementations=4.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..144074b8b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=NullableString_implementations=6.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..f22ee19c9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..3bb408b60 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..0eaa1cd61 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..bca347f7f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..14f7cb7ca --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=String_implementations=0.verified.txt @@ -0,0 +1,91 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..f71703b88 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=String_implementations=2.verified.txt @@ -0,0 +1,91 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..8f184ef8b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=String_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..4716a5da7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=String_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..0bfaf887d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Guid_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..62dac8852 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Guid_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..f4f5ad4d3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Guid_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..3f69dd57d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Guid_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..b91b2e666 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Int_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..1140332ec --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Int_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..fcd654e3b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..96e72ed42 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..61c040ae9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Long_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..fb1c8af42 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Long_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..83a4fdf2b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Long_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..1df1c38c4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=Long_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..0d4ed429d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=MassTransitNewId_implementations=0.verified.txt @@ -0,0 +1,134 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..bfb536815 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=MassTransitNewId_implementations=2.verified.txt @@ -0,0 +1,134 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..b7eb33df9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=MassTransitNewId_implementations=4.verified.txt @@ -0,0 +1,135 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..a5cf5cd7e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=MassTransitNewId_implementations=6.verified.txt @@ -0,0 +1,135 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..e94509185 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=NullableString_implementations=0.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..93ce166ef --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=NullableString_implementations=2.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..6690e2c88 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=NullableString_implementations=4.verified.txt @@ -0,0 +1,160 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..8a490181d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=NullableString_implementations=6.verified.txt @@ -0,0 +1,160 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..16172c01d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=0.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..4f76bc2fe --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=2.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..139a0957a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=4.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..906dc5d92 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=6.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..a2003f7dc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=String_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..3d2811abd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=String_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..22c65cd48 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=String_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..7bf8d7fa9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=String_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..7608a0149 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Guid_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..d0604df3e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Guid_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..825430f37 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Guid_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..895124269 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Guid_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..a0d6ef31b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Int_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..199a79419 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Int_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..2700c2777 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..0889f1106 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..0f4d555e8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Long_implementations=0.verified.txt @@ -0,0 +1,106 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..758a91fb2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Long_implementations=2.verified.txt @@ -0,0 +1,106 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..ecbb773e2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Long_implementations=4.verified.txt @@ -0,0 +1,107 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..ba98fa2ce --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=Long_implementations=6.verified.txt @@ -0,0 +1,107 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..742b0b20f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=MassTransitNewId_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..d5dbd867f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=MassTransitNewId_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..77ebd33d8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=MassTransitNewId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..73dc65405 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=MassTransitNewId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..e510a2fe0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=NullableString_implementations=0.verified.txt @@ -0,0 +1,134 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..92f37fa57 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=NullableString_implementations=2.verified.txt @@ -0,0 +1,134 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..2fd88b948 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=NullableString_implementations=4.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..912e1216a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=NullableString_implementations=6.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..bc367c45f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,134 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..40bf7b2e1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,134 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..08f7b7031 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,135 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..da67f92da --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,135 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..96ef4a5a0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=String_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..fcb403960 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=String_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..28a683459 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=String_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..171f0ab03 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=String_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..19924d32e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=0.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..f8c49ed65 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=2.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..6e1f7d3f4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=4.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.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..5691cd811 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=6.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.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..ae7657400 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Guid_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..749c351a3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Guid_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..16ad30155 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Guid_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..cd752027f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Guid_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..e6484387e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Int_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..398035a2f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Int_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..2ed573bed --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..0dc88ddb4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..e77a15f65 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Long_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..3284c0514 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Long_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..09cf27354 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Long_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..b078a4608 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=Long_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..411c63b36 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=MassTransitNewId_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..14f1e844f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=MassTransitNewId_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..9c749293c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=MassTransitNewId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..b6a5b2524 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=MassTransitNewId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..1890085ea --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=NullableString_implementations=0.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 +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..c7128b54c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=NullableString_implementations=2.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 +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..1fae0140f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=NullableString_implementations=4.verified.txt @@ -0,0 +1,187 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..4057d5e39 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=NullableString_implementations=6.verified.txt @@ -0,0 +1,187 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..0662a1f1e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=0.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..dd782afce --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=2.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..632b4d0c5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,178 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..df3915846 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,178 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..beb1e0912 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=String_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..24a91d2c5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=String_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..bdd1297a4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=String_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..f063156e2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=String_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..9e3799e6e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Guid_implementations=0.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..49b5ce9a1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Guid_implementations=2.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..dbcf7a682 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Guid_implementations=4.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..647748dfb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Guid_implementations=6.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..474db5b2a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Int_implementations=0.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..8caed72b0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Int_implementations=2.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..41989803d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Int_implementations=4.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..100d961f5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Int_implementations=6.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..fe9e96b3a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Long_implementations=0.verified.txt @@ -0,0 +1,82 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..4e45e9599 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Long_implementations=2.verified.txt @@ -0,0 +1,82 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..eb84e1374 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Long_implementations=4.verified.txt @@ -0,0 +1,83 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..002e992fb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=Long_implementations=6.verified.txt @@ -0,0 +1,83 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..ca4f3074a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=MassTransitNewId_implementations=0.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..0b522ba3c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=MassTransitNewId_implementations=2.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..c336838c5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=MassTransitNewId_implementations=4.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..f10fefb4f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=MassTransitNewId_implementations=6.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..556959aed --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=NullableString_implementations=0.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..8c477ee27 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=NullableString_implementations=2.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..28337c407 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=NullableString_implementations=4.verified.txt @@ -0,0 +1,107 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..72016cdcc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=NullableString_implementations=6.verified.txt @@ -0,0 +1,107 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..b6a730915 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=0.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..ca03bd036 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=2.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..3b43108cf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=4.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..91c4934f9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=6.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..b3a6c91ab --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=String_implementations=0.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..408947326 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=String_implementations=2.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..9f26d51d9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=String_implementations=4.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..0670bcc76 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=String_implementations=6.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..1864a255c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Guid_implementations=0.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..4156a14a5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Guid_implementations=2.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..fcb2f3ed9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Guid_implementations=4.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..4e6dc43a2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Guid_implementations=6.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..600238908 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Int_implementations=0.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..66d25dd1c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Int_implementations=2.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..b4f0bbb53 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Int_implementations=4.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..bc455f715 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Int_implementations=6.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..4d4b551a2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Long_implementations=0.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..5096b3410 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Long_implementations=2.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..962863cca --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Long_implementations=4.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..f4db0ed76 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=Long_implementations=6.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..fb8217db8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=MassTransitNewId_implementations=0.verified.txt @@ -0,0 +1,131 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..4a55a8810 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=MassTransitNewId_implementations=2.verified.txt @@ -0,0 +1,131 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..f984eec32 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=MassTransitNewId_implementations=4.verified.txt @@ -0,0 +1,132 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..82313dbba --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=MassTransitNewId_implementations=6.verified.txt @@ -0,0 +1,132 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..e7a5609c4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=NullableString_implementations=0.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 +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..ed39a49a0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=NullableString_implementations=2.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 +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..a75f91cce --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=NullableString_implementations=4.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 +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..fb2f53f37 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=NullableString_implementations=6.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 +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..35afc1bfc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=0.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..2e1b67d27 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=2.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..851f0d500 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=4.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 +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..cf12b103c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=6.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 +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..2e196bfc7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=String_implementations=0.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..8c43fa36a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=String_implementations=2.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..10f8152ae --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=String_implementations=4.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 +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..42a60415e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=String_implementations=6.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 +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..d728ba5c1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Guid_implementations=0.verified.txt @@ -0,0 +1,102 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..e2fdcd6f5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Guid_implementations=2.verified.txt @@ -0,0 +1,102 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..3eb65fc67 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Guid_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..8353e3519 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Guid_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..fee111726 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Int_implementations=0.verified.txt @@ -0,0 +1,102 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..4b2f37ba1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Int_implementations=2.verified.txt @@ -0,0 +1,102 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..894680dd2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..62e830dd2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..cf83841b0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Long_implementations=0.verified.txt @@ -0,0 +1,103 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..9f540083c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Long_implementations=2.verified.txt @@ -0,0 +1,103 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..6c17ceb4d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Long_implementations=4.verified.txt @@ -0,0 +1,104 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..7e27eae8f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=Long_implementations=6.verified.txt @@ -0,0 +1,104 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..7c18587bf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=MassTransitNewId_implementations=0.verified.txt @@ -0,0 +1,102 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..93ee123e7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=MassTransitNewId_implementations=2.verified.txt @@ -0,0 +1,102 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..08dad10d0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=MassTransitNewId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..2c37b3210 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=MassTransitNewId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..f087fa503 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=NullableString_implementations=0.verified.txt @@ -0,0 +1,124 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..306feb2a1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=NullableString_implementations=2.verified.txt @@ -0,0 +1,124 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..ae04efb3a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=NullableString_implementations=4.verified.txt @@ -0,0 +1,134 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..ff12e940d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=NullableString_implementations=6.verified.txt @@ -0,0 +1,134 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..a735dab0e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..92a942a6a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..9dbb4dd5e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..1c37a7f68 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..1f71da92c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=String_implementations=0.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 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..20eb0cc20 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=String_implementations=2.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 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..41b5c7435 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=String_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..1c686b26f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=String_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..bbd773df2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Guid_implementations=0.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..c50ee1983 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Guid_implementations=2.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..31a5440c5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Guid_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..90eec6667 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Guid_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..604f54251 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Int_implementations=0.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..0fb57dcad --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Int_implementations=2.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..e9d3d5986 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..190a29ab2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..1df1e4581 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Long_implementations=0.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..b24fefd57 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Long_implementations=2.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..86d53cdd3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Long_implementations=4.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..f86efe387 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=Long_implementations=6.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..e575b0b18 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=MassTransitNewId_implementations=0.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..4192b57c7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=MassTransitNewId_implementations=2.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..7e2d048cf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=MassTransitNewId_implementations=4.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..afe23f4a8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=MassTransitNewId_implementations=6.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..d5a9cb664 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=NullableString_implementations=0.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 +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..bbaa4d26e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=NullableString_implementations=2.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 +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..888bf9b76 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=NullableString_implementations=4.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 +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..cfa826bfc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=NullableString_implementations=6.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 +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..ab528e9cf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=0.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..af2211968 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=2.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..e98d4a936 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=4.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..86ed3587f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=6.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..5bf6e4647 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=String_implementations=0.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..8c92fa38f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=String_implementations=2.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..2212ff0f8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=String_implementations=4.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..4c0ceddbd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=String_implementations=6.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..8480079e7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Guid_implementations=0.verified.txt @@ -0,0 +1,95 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..54c923d08 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Guid_implementations=2.verified.txt @@ -0,0 +1,95 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..4cc2da4cc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Guid_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..e6e093206 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Guid_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..4dbb737ff --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Int_implementations=0.verified.txt @@ -0,0 +1,95 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..5701e9ce4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Int_implementations=2.verified.txt @@ -0,0 +1,95 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..94110da25 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..0b584e405 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..186a79c74 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Long_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..f5ad12032 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Long_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..5211cb009 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Long_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..89568dc06 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=Long_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..3d040e533 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=MassTransitNewId_implementations=0.verified.txt @@ -0,0 +1,95 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..6f35bbdab --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=MassTransitNewId_implementations=2.verified.txt @@ -0,0 +1,95 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..39e54b776 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=MassTransitNewId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..8a611d3a9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=MassTransitNewId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..9f6b45fb1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=NullableString_implementations=0.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..4346da19e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=NullableString_implementations=2.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..61c7bc706 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=NullableString_implementations=4.verified.txt @@ -0,0 +1,128 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..f04db153e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=NullableString_implementations=6.verified.txt @@ -0,0 +1,128 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..907c977b8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..422f6462f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..5382e0fd3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..b8af3dc37 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..7fe7e54ea --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=String_implementations=0.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..884f5c803 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=String_implementations=2.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..c60a320b9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=String_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..dff8a3bfb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=String_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..bb5a5ef4b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Guid_implementations=0.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..cb96e79a5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Guid_implementations=2.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..0db8b1bbe --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Guid_implementations=4.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..ff8f01151 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Guid_implementations=6.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..05e256440 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Int_implementations=0.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..20a4836da --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Int_implementations=2.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..afb968e3e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Int_implementations=4.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..db60a5676 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Int_implementations=6.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..18eb842ef --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Long_implementations=0.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..8a9c05394 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Long_implementations=2.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..2ed3361d9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Long_implementations=4.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..c4577970a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=Long_implementations=6.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..e2cdae19c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=MassTransitNewId_implementations=0.verified.txt @@ -0,0 +1,145 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..9db7a6332 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=MassTransitNewId_implementations=2.verified.txt @@ -0,0 +1,145 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..584fe029b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=MassTransitNewId_implementations=4.verified.txt @@ -0,0 +1,146 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..93254a739 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=MassTransitNewId_implementations=6.verified.txt @@ -0,0 +1,146 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..e2cc8eea0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=NullableString_implementations=0.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..acd967289 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=NullableString_implementations=2.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..440d692e2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=NullableString_implementations=4.verified.txt @@ -0,0 +1,171 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..01eeb4c4d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=NullableString_implementations=6.verified.txt @@ -0,0 +1,171 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..b974f86f4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=0.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..015eced30 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=2.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..68b79cdc4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,162 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..d2ee9cab5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,162 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..28a547f5c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=String_implementations=0.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..83c966a73 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=String_implementations=2.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..074724d76 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=String_implementations=4.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..61d4ef660 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=String_implementations=6.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..feaa207b5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Guid_implementations=0.verified.txt @@ -0,0 +1,116 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..552a5bcf7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Guid_implementations=2.verified.txt @@ -0,0 +1,116 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..aa1dc24f2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Guid_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..98cb1e6c8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Guid_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..991451aaa --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Int_implementations=0.verified.txt @@ -0,0 +1,116 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..5aebd3ccc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Int_implementations=2.verified.txt @@ -0,0 +1,116 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..c197d01b6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..25ee05566 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..6e75faff8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Long_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..1c23557d0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Long_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..2be60a756 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Long_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..97da56052 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=Long_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..4706e2085 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=MassTransitNewId_implementations=0.verified.txt @@ -0,0 +1,116 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..0521283b6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=MassTransitNewId_implementations=2.verified.txt @@ -0,0 +1,116 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..f37266d6d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=MassTransitNewId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..4d867be78 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=MassTransitNewId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..d7112bd9d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=NullableString_implementations=0.verified.txt @@ -0,0 +1,145 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..871fc711e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=NullableString_implementations=2.verified.txt @@ -0,0 +1,145 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..758ece953 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=NullableString_implementations=4.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..2e4776fd4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=NullableString_implementations=6.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..5e3d54e96 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,145 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..d00b1604a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,145 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..ae70492e9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,146 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..84d567a0a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,146 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..dedea1635 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=String_implementations=0.verified.txt @@ -0,0 +1,122 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..e6e738d9a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=String_implementations=2.verified.txt @@ -0,0 +1,122 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..b721b4805 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=String_implementations=4.verified.txt @@ -0,0 +1,132 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..8e635aaa3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=String_implementations=6.verified.txt @@ -0,0 +1,132 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..b3914ff61 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Guid_implementations=0.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..83902253c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Guid_implementations=2.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..c7e42bd2a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Guid_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..0495a0117 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Guid_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..f6af0c1cf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Int_implementations=0.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..07156954d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Int_implementations=2.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..76a816daf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..702702402 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..f89195119 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Long_implementations=0.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..d6f971a8a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Long_implementations=2.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..65c398493 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Long_implementations=4.verified.txt @@ -0,0 +1,162 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..adc79525c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=Long_implementations=6.verified.txt @@ -0,0 +1,162 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..0cadd13d8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=MassTransitNewId_implementations=0.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..915e3cf20 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=MassTransitNewId_implementations=2.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..f60acdb2d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=MassTransitNewId_implementations=4.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..a321af773 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=MassTransitNewId_implementations=6.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..0cc2a6b6a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=NullableString_implementations=0.verified.txt @@ -0,0 +1,188 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..8ff08c166 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=NullableString_implementations=2.verified.txt @@ -0,0 +1,188 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..40fc967a5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=NullableString_implementations=4.verified.txt @@ -0,0 +1,198 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..4326fa7e5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=NullableString_implementations=6.verified.txt @@ -0,0 +1,198 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..f33a94029 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,188 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..561fffcaa --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,188 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..eb0454015 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,189 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..7828a25c0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,189 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..fab409a72 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=String_implementations=0.verified.txt @@ -0,0 +1,160 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..55fabe29b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=String_implementations=2.verified.txt @@ -0,0 +1,160 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..15f28821f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=String_implementations=4.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..312b6e6c4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=String_implementations=6.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..1607fac4b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=0.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 + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..c337ee109 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=2.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.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..286e53c17 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=4.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..682ebff32 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=6.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..ab13cd74a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,131 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +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 MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..7ca904598 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,131 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +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 MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..78f552342 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,132 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +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 MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..4afc73b7d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,132 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +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 MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..936809ad4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,50 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..2590cfd59 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,50 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..bc835b8b1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,51 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..aa122e9a5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,51 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..16de8d48a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=0.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 MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..01ce0e706 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=2.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 MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..d2d51504e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=4.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 MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..cc4f11f52 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=6.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 MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..2aec1a34e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,77 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..111b2fec8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,77 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..e1a7754de --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,78 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..21f5d46bd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,78 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..f14d08fb5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=0.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..d4ec71de8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=2.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..ca3116b05 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=4.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..fbe393574 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=6.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..354c66d90 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=0.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..9b4db18e0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=2.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..872efd5b2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=4.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 MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..11106de1a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=6.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 MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..f7d6b51e1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,115 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..b5bcbf504 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,115 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..d42eb628a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,116 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..f9043b224 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,116 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..d50e970f9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=0.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 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..9c531fd53 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=2.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 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..9b113fe0e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=4.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.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..a2de8f716 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=6.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.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..04bd5ea00 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,82 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..f53b81178 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,82 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..5e670cb7d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,83 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..781e0bc04 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,83 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..9781a02ef --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=0.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 + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..e08bd1786 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=2.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.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..8dbfd6a20 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=4.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 + +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 MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..b398b194e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=6.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 + +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 MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..6b96687f7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=0.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 + +namespace Some.Namespace +{ + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..6eecbeefd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=2.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 + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..bea4f7dee --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=4.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 + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..7a35073e6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=6.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 + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..cfbcba487 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=0.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 +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..056dc9d44 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=2.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 +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..a39aa32ee --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=4.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.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..ad437f37c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=6.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.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..1d12e6e28 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=0.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 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..29965b640 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=2.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 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..67fd16818 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,85 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..e7b77b62b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,85 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..8ec8de183 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,127 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..28608d940 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,127 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..ee045605b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,128 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..0206ba250 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,128 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..b3ee5fabb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=0.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..5476f0598 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=2.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..1779a87be --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=4.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.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..ce337bc91 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=6.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.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..0042da41d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,122 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..f8c96a97b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,122 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..379eeadce --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,123 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..54b708901 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,123 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..38aaf3223 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,106 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..069181983 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,106 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..16ec89b08 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,107 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..8becbb788 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,107 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..da3783ce4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=0.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 MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..4cf7e8b55 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=2.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 MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..0dffcd215 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=4.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.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..fc671981e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=6.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.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..18132f41a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,68 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..36305bc9d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,68 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..d17c85845 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,69 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..e7dd7e217 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,69 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..a902afbad --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=0.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 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..d572266bd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=2.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 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..a9bf0834f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=4.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 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..7e1bfb232 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=6.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 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..64f1b80ad --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=0.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..b4ae0635b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=2.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..d0165ba3c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=4.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.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..cbc1bd4b0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=6.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.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..71ac4d604 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=0.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))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..873335891 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=2.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))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..6b6fe1fc8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=4.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 MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..7c442de5f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=6.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 MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..7d1892ec4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=0.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..34edc36ac --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=2.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..bfdddf99a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=4.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..c212c2bf6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=6.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..811969e3b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=0.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 + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..676e302ab --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=2.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.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..f94fe7e39 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=4.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.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..43fd9fd24 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=6.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.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..4ddea4d7e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,133 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..13ff81e51 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,133 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..caead2ffd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,134 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..4d0552ffb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,134 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..a7c8dc8c8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=0.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))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..0936bb50e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=2.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))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..0250ffd9d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=4.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))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..141948ad2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=6.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))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..e2d02e0d2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,160 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 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 MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..09687e781 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,160 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 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 MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..a4a770fa0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=4.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 + +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 MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..9e396c185 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=6.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 + +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 MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..a1b135087 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Guid_implementations=0.verified.txt @@ -0,0 +1,52 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..46d1cf59e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Guid_implementations=2.verified.txt @@ -0,0 +1,52 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..08337b258 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Guid_implementations=4.verified.txt @@ -0,0 +1,53 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..e576a0d79 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Guid_implementations=6.verified.txt @@ -0,0 +1,53 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..e8c368859 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Int_implementations=0.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..ec7b50461 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Int_implementations=2.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..d4b49f7b1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Int_implementations=4.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..5ad864df8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Int_implementations=6.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..d124a22fe --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Long_implementations=0.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..dd9e4a266 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Long_implementations=2.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..12da83a63 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Long_implementations=4.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..7869c9973 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=Long_implementations=6.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..d69b7212c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=MassTransitNewId_implementations=0.verified.txt @@ -0,0 +1,52 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..2cf44a8f8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=MassTransitNewId_implementations=2.verified.txt @@ -0,0 +1,52 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..960b44b8c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=MassTransitNewId_implementations=4.verified.txt @@ -0,0 +1,53 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..65c202084 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=MassTransitNewId_implementations=6.verified.txt @@ -0,0 +1,53 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..7a7913d15 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=NullableString_implementations=0.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 +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..d106a61d6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=NullableString_implementations=2.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 +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..ebd84e1ba --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=NullableString_implementations=4.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 +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..cd155d6e2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=NullableString_implementations=6.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 +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..5d34206ae --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=0.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..a536c87af --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=2.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..3918d4579 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=4.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 + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..fd1f4aa98 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=6.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 + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..7989dc2c2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=String_implementations=0.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..d7ab188d0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=String_implementations=2.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..e19d62624 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=String_implementations=4.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..353f14759 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=String_implementations=6.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..124594e94 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Guid_implementations=0.verified.txt @@ -0,0 +1,94 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..5a12fcf44 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Guid_implementations=2.verified.txt @@ -0,0 +1,94 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..b88ec67e8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Guid_implementations=4.verified.txt @@ -0,0 +1,95 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..735f53777 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Guid_implementations=6.verified.txt @@ -0,0 +1,95 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..4dedca4fd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Int_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..ac09c40bb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Int_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..8d4a29b09 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..e11697657 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..5bd910b86 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Long_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..ddec2bf7b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Long_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..a55c2bbfb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Long_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..7fc6d5283 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=Long_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..3d04a78c2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=MassTransitNewId_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..a91bffb60 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=MassTransitNewId_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..669be42ee --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=MassTransitNewId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..33be3eb5c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=MassTransitNewId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..a7e376da9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=NullableString_implementations=0.verified.txt @@ -0,0 +1,110 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..69be25550 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=NullableString_implementations=2.verified.txt @@ -0,0 +1,110 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..4f750a2ef --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=NullableString_implementations=4.verified.txt @@ -0,0 +1,120 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..495e97ab2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=NullableString_implementations=6.verified.txt @@ -0,0 +1,120 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..69ae48dc0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,110 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..154f00b84 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,110 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..fc3f5e975 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,111 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..40fc4cf07 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,111 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..c10467243 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=String_implementations=0.verified.txt @@ -0,0 +1,98 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..86035ce2a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=String_implementations=2.verified.txt @@ -0,0 +1,98 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..2e8477ace --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=String_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..bee4dddc6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=String_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..a8f53e1ce --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Guid_implementations=0.verified.txt @@ -0,0 +1,73 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..4bcdcc59c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Guid_implementations=2.verified.txt @@ -0,0 +1,73 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..9e82fb47b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Guid_implementations=4.verified.txt @@ -0,0 +1,74 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..db805d8f3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Guid_implementations=6.verified.txt @@ -0,0 +1,74 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..6bcd13c6c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Int_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..88b7f51f1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Int_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..b43838446 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..688a2bcc8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..0572f49c4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Long_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..81a6eb590 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Long_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..4df25b801 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Long_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..152c6b37a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=Long_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..cda50e859 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=MassTransitNewId_implementations=0.verified.txt @@ -0,0 +1,73 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..9f430205d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=MassTransitNewId_implementations=2.verified.txt @@ -0,0 +1,73 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..4e9213684 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=MassTransitNewId_implementations=4.verified.txt @@ -0,0 +1,74 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..e5df7237e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=MassTransitNewId_implementations=6.verified.txt @@ -0,0 +1,74 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..9f26d54ec --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=NullableString_implementations=0.verified.txt @@ -0,0 +1,94 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..857130d87 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=NullableString_implementations=2.verified.txt @@ -0,0 +1,94 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..906d5cfc2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=NullableString_implementations=4.verified.txt @@ -0,0 +1,104 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..02b6b4ebd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=NullableString_implementations=6.verified.txt @@ -0,0 +1,104 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..55121a51d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,94 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..5f1844f6a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,94 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..806520188 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,95 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..42003d870 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,95 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..791af046f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=String_implementations=0.verified.txt @@ -0,0 +1,80 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..a0705e44b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=String_implementations=2.verified.txt @@ -0,0 +1,80 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..9b5674421 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=String_implementations=4.verified.txt @@ -0,0 +1,90 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..bc47e38ea --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=String_implementations=6.verified.txt @@ -0,0 +1,90 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..da34c0ef0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=0.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 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..4d2a3bb46 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=2.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 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..7e8e5f205 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=4.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..c29ff7770 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=6.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..48e2b682e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Guid_implementations=0.verified.txt @@ -0,0 +1,115 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..ab43dfb93 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Guid_implementations=2.verified.txt @@ -0,0 +1,115 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..656721be9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Guid_implementations=4.verified.txt @@ -0,0 +1,116 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..b901a5704 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Guid_implementations=6.verified.txt @@ -0,0 +1,116 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..3c595f44b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Int_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..7a1696690 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Int_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..3f333332a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..7c1252c48 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..3e3b8fdd6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Long_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..7cdd19d45 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Long_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..93edbc4d2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Long_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..7482a24c0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=Long_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..f6c848f50 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=MassTransitNewId_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..6973ee88f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=MassTransitNewId_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..af43dd84b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=MassTransitNewId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..b53c35c46 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=MassTransitNewId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..bca38a63a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=NullableString_implementations=0.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..f5176de94 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=NullableString_implementations=2.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..7ff8e6b7b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=NullableString_implementations=4.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 +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..810efd17e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=NullableString_implementations=6.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 +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..95f0889d5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..f9f63919e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..c629fe564 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=4.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..12919ab03 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=6.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..9614fc518 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=String_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..9584e5f09 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=String_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..4b5d90e16 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=String_implementations=4.verified.txt @@ -0,0 +1,128 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..648cf1eb7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=String_implementations=6.verified.txt @@ -0,0 +1,128 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..b8abcb2d3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Guid_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..66b52a96e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Guid_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..7912a88bf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Guid_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..c7a04ffe6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Guid_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..f89925737 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Int_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..6447f0265 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Int_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..b837828fc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..b3044fc92 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..8e2dad4cf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Long_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..174cd8eae --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Long_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..b059d068f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Long_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..f5d066add --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=Long_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..214657064 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=MassTransitNewId_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..14186cb9e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=MassTransitNewId_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..cb1a8cc4d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=MassTransitNewId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..259184caf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=MassTransitNewId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..ff9e9b0a8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=NullableString_implementations=0.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 +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..aeb412356 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=NullableString_implementations=2.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 +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..26c79eb24 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=NullableString_implementations=4.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..12fbd7906 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=NullableString_implementations=6.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..a47042d18 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,89 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..68040e96e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,89 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..943eb6343 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,90 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..f2a600f09 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,90 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..fe190fc94 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=String_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..41642b081 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=String_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..f52a39de1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=String_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..5d8f9887a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=String_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..786edab69 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Guid_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..0bb0f12bc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Guid_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..f3a38679d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Guid_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..ce3abde5a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Guid_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..1a5de31d7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Int_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..f794ea77b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Int_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..97446de4a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..6390b7a1c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..e615923e6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Long_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..9da5894a3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Long_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..bf63a9d41 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Long_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..629e90f7e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=Long_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..e906134fc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=MassTransitNewId_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..d42d38d5e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=MassTransitNewId_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..57e12ffe9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=MassTransitNewId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..7d2a5fbc0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=MassTransitNewId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..359c83f25 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=NullableString_implementations=0.verified.txt @@ -0,0 +1,131 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..f468fda4e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=NullableString_implementations=2.verified.txt @@ -0,0 +1,131 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..5adf3e114 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=NullableString_implementations=4.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..d7c45e0b1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=NullableString_implementations=6.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..57f35fc0e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=0.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..379b4cf90 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=2.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..ab64cf45a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=4.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..dabdafbad --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=6.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..8ac423edd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=String_implementations=0.verified.txt @@ -0,0 +1,112 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..f1dee1ee0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=String_implementations=2.verified.txt @@ -0,0 +1,112 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..b1a83a2b5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=String_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..b4dae0982 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=String_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..a8b00e4ae --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Guid_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..ebde307f8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Guid_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..f7fa33255 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Guid_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..b13619834 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Guid_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..4976b589b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Int_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..7dbd7eb3b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Int_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..ec3f63b2c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..8e2718664 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..c4e109bc1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Long_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..86e8580e1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Long_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..6e53f7f64 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Long_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..84b5e167f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=Long_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..11948c73a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=MassTransitNewId_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..8c3f8933f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=MassTransitNewId_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..e40573fc7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=MassTransitNewId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..3291613d8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=MassTransitNewId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..d4f0780a6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=NullableString_implementations=0.verified.txt @@ -0,0 +1,115 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..411fb65d7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=NullableString_implementations=2.verified.txt @@ -0,0 +1,115 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..c4a786b8e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=NullableString_implementations=4.verified.txt @@ -0,0 +1,125 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..e59a1a570 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=NullableString_implementations=6.verified.txt @@ -0,0 +1,125 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..579f44b66 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,116 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..210b5e58d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,116 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..270590345 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..e6c0afd37 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..5dbe77975 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=String_implementations=0.verified.txt @@ -0,0 +1,94 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..1b9282b6c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=String_implementations=2.verified.txt @@ -0,0 +1,94 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..37b2ea41d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=String_implementations=4.verified.txt @@ -0,0 +1,104 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..876afa9e3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=String_implementations=6.verified.txt @@ -0,0 +1,104 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..4bbd80b6b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Guid_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..26304c0e2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Guid_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..fe9d66b69 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Guid_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..0ee560d65 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Guid_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..0d37b3e07 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Int_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..d93423a08 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Int_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..33a55856e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..2f7a9adc9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..13067d231 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Long_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..8074f3cf9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Long_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..56af0e0a7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Long_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..2212123b8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=Long_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..ebd1d4452 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=MassTransitNewId_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..86c460013 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=MassTransitNewId_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..7a6300fdc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=MassTransitNewId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..e6b16236f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=MassTransitNewId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..9069d2a2e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=NullableString_implementations=0.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 +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..7fa9fd654 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=NullableString_implementations=2.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 +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..6de85c89b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=NullableString_implementations=4.verified.txt @@ -0,0 +1,168 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..4a2ef7b34 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=NullableString_implementations=6.verified.txt @@ -0,0 +1,168 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..6d5b97c8f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..25ab8031d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..d144203fb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,160 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..6f321ddaf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,160 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..e24e0a01e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=String_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..abde41fdf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=String_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..91a223e51 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=String_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..6ac7d926a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=String_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..edf637f59 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Guid_implementations=0.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..501c8195c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Guid_implementations=2.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..5cc432192 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Guid_implementations=4.verified.txt @@ -0,0 +1,64 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..7ba42a0c5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Guid_implementations=6.verified.txt @@ -0,0 +1,64 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..fe9c9e30c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Int_implementations=0.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..5ef55ba45 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Int_implementations=2.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..84ba390fd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Int_implementations=4.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..0fa696883 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Int_implementations=6.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..1378ee57f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Long_implementations=0.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..b65747659 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Long_implementations=2.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..165766807 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Long_implementations=4.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..902a55594 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=Long_implementations=6.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..63c479887 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=MassTransitNewId_implementations=0.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..fbc381097 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=MassTransitNewId_implementations=2.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..a37784e9f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=MassTransitNewId_implementations=4.verified.txt @@ -0,0 +1,64 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..dad00764a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=MassTransitNewId_implementations=6.verified.txt @@ -0,0 +1,64 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..ab4e1083c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=NullableString_implementations=0.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..bbbb22a90 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=NullableString_implementations=2.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..267e046f5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=NullableString_implementations=4.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 +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..330e22868 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=NullableString_implementations=6.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 +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..28eb2e284 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=0.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 + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..c26cef75f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=2.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 + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..544f0ddc0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=4.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..4b8b86738 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=6.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..c5399756a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=String_implementations=0.verified.txt @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..f6688a7c0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=String_implementations=2.verified.txt @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..1ab940ff8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=String_implementations=4.verified.txt @@ -0,0 +1,81 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..06ca093a6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=String_implementations=6.verified.txt @@ -0,0 +1,81 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..281f9261b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Guid_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..adc1d35d0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Guid_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..394d1f0d3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Guid_implementations=4.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..6255d9c40 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Guid_implementations=6.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..4938c4584 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Int_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..b0c36f747 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Int_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..473c44293 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..769332b07 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..60ef741fb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Long_implementations=0.verified.txt @@ -0,0 +1,106 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..482ab8db9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Long_implementations=2.verified.txt @@ -0,0 +1,106 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..2624c22c6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Long_implementations=4.verified.txt @@ -0,0 +1,107 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..9912cbdef --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=Long_implementations=6.verified.txt @@ -0,0 +1,107 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..ff17fd453 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=MassTransitNewId_implementations=0.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..25b68f79c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=MassTransitNewId_implementations=2.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..6a2ec0996 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=MassTransitNewId_implementations=4.verified.txt @@ -0,0 +1,114 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..872fe7b94 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=MassTransitNewId_implementations=6.verified.txt @@ -0,0 +1,114 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..b27a6ddb1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=NullableString_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..eb87687a7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=NullableString_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..d514ef4c0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=NullableString_implementations=4.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 +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..8ba7fd62d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=NullableString_implementations=6.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 +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..757314d67 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..b0013e194 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..5e15f210f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=4.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..7595847c1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=6.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..e7e673632 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=String_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..c8aefb4c8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=String_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..553e73200 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=String_implementations=4.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..4a447c644 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=String_implementations=6.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..4b68ff872 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Guid_implementations=0.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 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..3c21691b4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Guid_implementations=2.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 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..acb44ca65 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Guid_implementations=4.verified.txt @@ -0,0 +1,85 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..66e8a50c2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Guid_implementations=6.verified.txt @@ -0,0 +1,85 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..7175d5c7c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Int_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..d47e0ad6a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Int_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..e44f7120f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Int_implementations=4.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 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..8046dc799 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Int_implementations=6.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 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..4a5562d90 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Long_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..98f4e47f8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Long_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..780ae96fe --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Long_implementations=4.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 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..4c26a1eae --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=Long_implementations=6.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 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..9cf4aab34 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=MassTransitNewId_implementations=0.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 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..934362bbf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=MassTransitNewId_implementations=2.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 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..17a991633 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=MassTransitNewId_implementations=4.verified.txt @@ -0,0 +1,85 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..78d185c75 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=MassTransitNewId_implementations=6.verified.txt @@ -0,0 +1,85 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..3e9fec719 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=NullableString_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..50eb81151 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=NullableString_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..b328a5acf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=NullableString_implementations=4.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 +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..43ecf3490 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=NullableString_implementations=6.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 +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..b594f689e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..bf58a0061 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..94f03e0f3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..5809c8a99 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..a07a4cba8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=String_implementations=0.verified.txt @@ -0,0 +1,91 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..30e34aa88 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=String_implementations=2.verified.txt @@ -0,0 +1,91 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..4bbf1e60f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=String_implementations=4.verified.txt @@ -0,0 +1,101 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..8b1c6e1e0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=String_implementations=6.verified.txt @@ -0,0 +1,101 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..752e4642d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Guid_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..5d5845d61 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Guid_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..bb0d461e8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Guid_implementations=4.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..33c80f68f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Guid_implementations=6.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..9c3b545e7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Int_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..6656ee6fd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Int_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..a16cee910 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..201ff5a5e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..8584ec536 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Long_implementations=0.verified.txt @@ -0,0 +1,127 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..8badbb2f0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Long_implementations=2.verified.txt @@ -0,0 +1,127 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..68537e223 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Long_implementations=4.verified.txt @@ -0,0 +1,128 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..f74c3c671 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=Long_implementations=6.verified.txt @@ -0,0 +1,128 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..2f640d722 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=MassTransitNewId_implementations=0.verified.txt @@ -0,0 +1,134 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..692901d63 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=MassTransitNewId_implementations=2.verified.txt @@ -0,0 +1,134 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..f026155da --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=MassTransitNewId_implementations=4.verified.txt @@ -0,0 +1,135 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..d5e2cf616 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=MassTransitNewId_implementations=6.verified.txt @@ -0,0 +1,135 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..d130356dd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=NullableString_implementations=0.verified.txt @@ -0,0 +1,148 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..d245533b4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=NullableString_implementations=2.verified.txt @@ -0,0 +1,148 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..5d4f2ed5c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=NullableString_implementations=4.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 +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..98a82b4d9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=NullableString_implementations=6.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 +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..4b1f91f59 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,148 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..21798830f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,148 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..cc2429c32 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,149 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..2c6f11c52 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,149 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..5e4377040 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=String_implementations=0.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..0c49b3226 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=String_implementations=2.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..7b82b24e2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=String_implementations=4.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..940624d91 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=String_implementations=6.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..0a5dcacbb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Guid_implementations=0.verified.txt @@ -0,0 +1,77 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..cbe09f131 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Guid_implementations=2.verified.txt @@ -0,0 +1,77 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..c28ee9928 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Guid_implementations=4.verified.txt @@ -0,0 +1,78 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..87d353045 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Guid_implementations=6.verified.txt @@ -0,0 +1,78 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..5e17c9d76 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Int_implementations=0.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..6dbcf80a6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Int_implementations=2.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..1a5c97a62 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Int_implementations=4.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..ca7c062c4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Int_implementations=6.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..ae50fdf17 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Long_implementations=0.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..df61db1e4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Long_implementations=2.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..7c7acd7f3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Long_implementations=4.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..9d5a50f6b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=Long_implementations=6.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..d30112431 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=MassTransitNewId_implementations=0.verified.txt @@ -0,0 +1,77 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..877fe7ab5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=MassTransitNewId_implementations=2.verified.txt @@ -0,0 +1,77 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..20dacd508 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=MassTransitNewId_implementations=4.verified.txt @@ -0,0 +1,78 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..127dec11e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=MassTransitNewId_implementations=6.verified.txt @@ -0,0 +1,78 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..4e376e304 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=NullableString_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..e695f866c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=NullableString_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..0e48d5549 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=NullableString_implementations=4.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..6e8303d2f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=NullableString_implementations=6.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..6d7116a79 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=0.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..9643086f0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=2.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..b662eed99 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..786fa7e40 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..5c331e400 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=String_implementations=0.verified.txt @@ -0,0 +1,85 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..6c57ec433 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=String_implementations=2.verified.txt @@ -0,0 +1,85 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..64a33d2ac --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=String_implementations=4.verified.txt @@ -0,0 +1,95 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..5ae5b2f5d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=String_implementations=6.verified.txt @@ -0,0 +1,95 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..96551e3b2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,61 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..dc5ccc772 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,61 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..601afddc6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,62 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..1846e3647 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,62 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..463c7e67a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Guid_implementations=0.verified.txt @@ -0,0 +1,119 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..4a90f47e6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Guid_implementations=2.verified.txt @@ -0,0 +1,119 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..e3e24985d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Guid_implementations=4.verified.txt @@ -0,0 +1,120 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..b17f25d03 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Guid_implementations=6.verified.txt @@ -0,0 +1,120 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..49b339c63 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Int_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..3de115518 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Int_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..a9c15c945 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..2e09a2bc5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..454563f5c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Long_implementations=0.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..1504c527c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Long_implementations=2.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..cbffc6156 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Long_implementations=4.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..db7b94727 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=Long_implementations=6.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..3dc4b7563 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=MassTransitNewId_implementations=0.verified.txt @@ -0,0 +1,127 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..6885a6d2e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=MassTransitNewId_implementations=2.verified.txt @@ -0,0 +1,127 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..7476041cb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=MassTransitNewId_implementations=4.verified.txt @@ -0,0 +1,128 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..9ae74a2a9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=MassTransitNewId_implementations=6.verified.txt @@ -0,0 +1,128 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..c2a3da9a6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=NullableString_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + 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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..778fae7c9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=NullableString_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + 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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..349996dee --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=NullableString_implementations=4.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + 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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..5ee6a660f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=NullableString_implementations=6.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 + +#nullable enable +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + 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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..79961e06d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=0.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..be24df22e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=2.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 + +namespace Some.Namespace +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..bd138b95a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=4.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..c80fb8982 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=6.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..309427bdb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=String_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..9af261f2d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=String_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..c8ecf9695 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=String_implementations=4.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..3c828cf7f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=String_implementations=6.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 +{ + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..5e971e1b1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Guid_implementations=0.verified.txt @@ -0,0 +1,98 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..b0a0353a8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Guid_implementations=2.verified.txt @@ -0,0 +1,98 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..55870bd76 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Guid_implementations=4.verified.txt @@ -0,0 +1,99 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..de82c13ff --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Guid_implementations=6.verified.txt @@ -0,0 +1,99 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..273aeeaac --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Int_implementations=0.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))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..c004f7b2c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Int_implementations=2.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))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..7fa95c056 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Int_implementations=4.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 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..adca49894 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Int_implementations=6.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 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..b6df63859 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Long_implementations=0.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))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..f8b70524b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Long_implementations=2.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))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..dd54a9499 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Long_implementations=4.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 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..e3621ab32 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=Long_implementations=6.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 +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..eecb72c10 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=MassTransitNewId_implementations=0.verified.txt @@ -0,0 +1,98 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..6adad6dc7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=MassTransitNewId_implementations=2.verified.txt @@ -0,0 +1,98 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..2e5ca369d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=MassTransitNewId_implementations=4.verified.txt @@ -0,0 +1,99 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..d4c7c691d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=MassTransitNewId_implementations=6.verified.txt @@ -0,0 +1,99 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..25e2d8475 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=NullableString_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..a164cb552 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=NullableString_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..3adc78ce5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=NullableString_implementations=4.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..67b67de95 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=NullableString_implementations=6.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 + +#nullable enable +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..6b2f0148f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,127 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..018bc4d1d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,127 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..c6309dd05 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,128 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..ff44c4b90 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,128 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..51af19a0e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=String_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..35f5ed2c4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=String_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..f550a7f72 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=String_implementations=4.verified.txt @@ -0,0 +1,115 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..ee1dd10be --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=String_implementations=6.verified.txt @@ -0,0 +1,115 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..314466a2e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Guid_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..032f65867 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Guid_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..c234309ca --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Guid_implementations=4.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..c94005db4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Guid_implementations=6.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..128caa97f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Int_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..75126be32 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Int_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..cbd56421c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..93263a119 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..e64511ae0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Long_implementations=0.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..046bd9765 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Long_implementations=2.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..016454bbf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Long_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..110386276 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=Long_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..25fb63467 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=MassTransitNewId_implementations=0.verified.txt @@ -0,0 +1,148 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..52e2f568e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=MassTransitNewId_implementations=2.verified.txt @@ -0,0 +1,148 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..039f6c326 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=MassTransitNewId_implementations=4.verified.txt @@ -0,0 +1,149 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..9e713e775 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=MassTransitNewId_implementations=6.verified.txt @@ -0,0 +1,149 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..afbd5d159 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=NullableString_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + 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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..fcc22a042 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=NullableString_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + 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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..f953f856e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=NullableString_implementations=4.verified.txt @@ -0,0 +1,179 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + 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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..c6f84f03b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=NullableString_implementations=6.verified.txt @@ -0,0 +1,179 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + 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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..21513e9a3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=0.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..e6f27daac --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=2.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..452cd398d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,171 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..a79f8ef7c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,171 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..7a59b3df9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=String_implementations=0.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..984edfd49 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=String_implementations=2.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..d28c91fa7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=String_implementations=4.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..65c793f21 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=String_implementations=6.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 + +namespace Some.Namespace +{ + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..83f1e5ca6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Guid_implementations=0.verified.txt @@ -0,0 +1,70 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..090968fe9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Guid_implementations=2.verified.txt @@ -0,0 +1,70 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..1c417f9c2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Guid_implementations=4.verified.txt @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..30ca1e8ce --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Guid_implementations=6.verified.txt @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..b7e020d3c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Int_implementations=0.verified.txt @@ -0,0 +1,70 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..58347b9f3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Int_implementations=2.verified.txt @@ -0,0 +1,70 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..70aac5736 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Int_implementations=4.verified.txt @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..f66018e77 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Int_implementations=6.verified.txt @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..4598e4b05 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Long_implementations=0.verified.txt @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..185620e56 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Long_implementations=2.verified.txt @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..b37a661ee --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Long_implementations=4.verified.txt @@ -0,0 +1,72 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..241ed0d59 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=Long_implementations=6.verified.txt @@ -0,0 +1,72 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..9d1ff366c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=MassTransitNewId_implementations=0.verified.txt @@ -0,0 +1,70 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..6a92e0879 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=MassTransitNewId_implementations=2.verified.txt @@ -0,0 +1,70 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..41c4b1b3b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=MassTransitNewId_implementations=4.verified.txt @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..90068cc92 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=MassTransitNewId_implementations=6.verified.txt @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..f1b881c7b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=NullableString_implementations=0.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..8d85562b9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=NullableString_implementations=2.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..169892f33 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=NullableString_implementations=4.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 +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..ebed29d48 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=NullableString_implementations=6.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 +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..fe5a05b0c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,85 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..d4963b9d9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,85 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..009dcb47a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,86 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..24f623155 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,86 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..8c3a5ee00 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=String_implementations=0.verified.txt @@ -0,0 +1,77 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..f2ef8d5f0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=String_implementations=2.verified.txt @@ -0,0 +1,77 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..0dd26108a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=String_implementations=4.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..f316a9053 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=String_implementations=6.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 +{ + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..44ac11b53 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Guid_implementations=0.verified.txt @@ -0,0 +1,112 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..e0ad51e91 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Guid_implementations=2.verified.txt @@ -0,0 +1,112 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..2b63ea6aa --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Guid_implementations=4.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..11c020bc4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Guid_implementations=6.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..880771379 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Int_implementations=0.verified.txt @@ -0,0 +1,112 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..24a25b5c2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Int_implementations=2.verified.txt @@ -0,0 +1,112 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..595beff0d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Int_implementations=4.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..6ab0fd26f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Int_implementations=6.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..bcaeb4e33 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Long_implementations=0.verified.txt @@ -0,0 +1,115 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..3a1bdf216 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Long_implementations=2.verified.txt @@ -0,0 +1,115 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..5c92af473 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Long_implementations=4.verified.txt @@ -0,0 +1,116 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..424e2cc7f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=Long_implementations=6.verified.txt @@ -0,0 +1,116 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..1c9e9160c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=MassTransitNewId_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..dbd0d4e37 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=MassTransitNewId_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..e3abd85ef --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=MassTransitNewId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..b8611834a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=MassTransitNewId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..18fde2c50 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=NullableString_implementations=0.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 +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..d98ea4c61 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=NullableString_implementations=2.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 +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..35fd9ab08 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=NullableString_implementations=4.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 +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..ab39eaf98 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=NullableString_implementations=6.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 +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..acf664d88 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=0.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..467621541 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=2.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..d8103c622 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=4.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..feb59da8e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=6.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..3d162b016 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=String_implementations=0.verified.txt @@ -0,0 +1,115 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..8a008b589 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=String_implementations=2.verified.txt @@ -0,0 +1,115 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..aa42a9bcd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=String_implementations=4.verified.txt @@ -0,0 +1,125 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..a08425c37 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=String_implementations=6.verified.txt @@ -0,0 +1,125 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +namespace Some.Namespace +{ + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } +} diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..921686a2d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,36 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..3217d3090 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,36 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..b51b79f13 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=ObjectId_implementations=4.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.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..b7c6a61cc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=ObjectId_implementations=6.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.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..410b4fe1a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Guid_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..c5e767eb2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Guid_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..6134ea16d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Guid_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..cafd5df56 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Guid_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..4ea5e9f95 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Int_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..9f964a101 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Int_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..26407816b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..8cb336a58 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..8110e55d5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Long_implementations=0.verified.txt @@ -0,0 +1,89 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..e5c7c5b4d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Long_implementations=2.verified.txt @@ -0,0 +1,89 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..931ddb307 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Long_implementations=4.verified.txt @@ -0,0 +1,90 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..218ec7e55 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=Long_implementations=6.verified.txt @@ -0,0 +1,90 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..7bb6728af --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=MassTransitNewId_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..5b79bd252 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=MassTransitNewId_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..d1991ef1a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=MassTransitNewId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..52b313e10 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=MassTransitNewId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..d6aa68ea8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=NullableString_implementations=0.verified.txt @@ -0,0 +1,110 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..a359c7717 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=NullableString_implementations=2.verified.txt @@ -0,0 +1,110 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..6605b1491 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=NullableString_implementations=4.verified.txt @@ -0,0 +1,120 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..96ae263d6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=NullableString_implementations=6.verified.txt @@ -0,0 +1,120 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..bacfe62ad --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..9baca3eff --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..1eb98819d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..c9545a267 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..5b5d8a427 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=String_implementations=0.verified.txt @@ -0,0 +1,94 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..e0a082309 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=String_implementations=2.verified.txt @@ -0,0 +1,94 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..6b788b1a9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=String_implementations=4.verified.txt @@ -0,0 +1,104 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..4bfbc74d4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=String_implementations=6.verified.txt @@ -0,0 +1,104 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..fa9dc66b9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Guid_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..a488791f8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Guid_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..ee8732033 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Guid_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..8d9400838 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Guid_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..837156991 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Int_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..980374640 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Int_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..930f417ba --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..3968f023d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..61d1184cf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Long_implementations=0.verified.txt @@ -0,0 +1,133 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..2ffef4e9d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Long_implementations=2.verified.txt @@ -0,0 +1,133 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..e3df6bfa9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Long_implementations=4.verified.txt @@ -0,0 +1,134 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..bc2df4d9f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=Long_implementations=6.verified.txt @@ -0,0 +1,134 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..8a51e83bf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=MassTransitNewId_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..54e020fe2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=MassTransitNewId_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..f2f5672a0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=MassTransitNewId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..aa8403b85 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=MassTransitNewId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..8b74870c3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=NullableString_implementations=0.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 + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..a690bfe8a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=NullableString_implementations=2.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 + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..75ce1e2d7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=NullableString_implementations=4.verified.txt @@ -0,0 +1,163 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..5bae7b1bb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=NullableString_implementations=6.verified.txt @@ -0,0 +1,163 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..9e10cc0a6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=0.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..272f643ac --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=2.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..c3b9860ee --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=4.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..c69451203 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=6.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..e88cd5626 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=String_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..e4e294913 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=String_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..68c3a62ce --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=String_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..b2a79f773 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=String_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..86b6e3f3c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Guid_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..57f6e613f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Guid_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..8a8471996 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Guid_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..712a14b87 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Guid_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..37f282538 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Int_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..8b17cb12b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Int_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..03dec64dc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..8c2104894 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..f8e06c2ed --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Long_implementations=0.verified.txt @@ -0,0 +1,82 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..d87f82662 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Long_implementations=2.verified.txt @@ -0,0 +1,82 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..6c0f93322 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Long_implementations=4.verified.txt @@ -0,0 +1,83 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..4bd6f8e95 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=Long_implementations=6.verified.txt @@ -0,0 +1,83 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..b884a741b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=MassTransitNewId_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..0fc09290a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=MassTransitNewId_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..503375903 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=MassTransitNewId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..76ef863ad --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=MassTransitNewId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..59a666e40 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=NullableString_implementations=0.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 + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..359d36aea --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=NullableString_implementations=2.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 + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..14522968c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=NullableString_implementations=4.verified.txt @@ -0,0 +1,114 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..1a33dd760 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=NullableString_implementations=6.verified.txt @@ -0,0 +1,114 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..15c7e8834 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..0fc03749f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..b86c8d3c3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..71fac4eda --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..f8905e53b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=String_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..a2c3a473f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=String_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..bd1f848cd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=String_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..a1e826d93 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=String_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..e475e5c66 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Guid_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..b968952b4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Guid_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..2e767498a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Guid_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..7d145d3b1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Guid_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..0f96d435d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Int_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..5fc7743a2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Int_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..ff96def72 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..3b2075a5a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..f8642245f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Long_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..5ea924393 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Long_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..138aa59a2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Long_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..11360ebd2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=Long_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..249dff9ae --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=MassTransitNewId_implementations=0.verified.txt @@ -0,0 +1,131 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..3446b406e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=MassTransitNewId_implementations=2.verified.txt @@ -0,0 +1,131 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..1ad890f56 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=MassTransitNewId_implementations=4.verified.txt @@ -0,0 +1,132 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..ac8f5ecf9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=MassTransitNewId_implementations=6.verified.txt @@ -0,0 +1,132 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..d3fd0d0cb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=NullableString_implementations=0.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 + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..613cfea1a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=NullableString_implementations=2.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 + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..d5dd3b1e2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=NullableString_implementations=4.verified.txt @@ -0,0 +1,157 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..4656bf08b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=NullableString_implementations=6.verified.txt @@ -0,0 +1,157 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..9a27dd6dc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=0.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..a7bd1b439 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=2.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..1df765502 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=4.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..6a9906212 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=6.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..b06d83f3f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=String_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..8666170f4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=String_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..e6f6d1e57 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=String_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..9f83e840d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=String_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..ddd08c258 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Guid_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..f43101741 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Guid_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..e34dc487a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Guid_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..e78c4d08e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Guid_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..a957ae3be --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Int_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..e6df87336 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Int_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..002348d79 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..5b9214cb5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..88fec49f4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Long_implementations=0.verified.txt @@ -0,0 +1,103 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..ccfbd20d7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Long_implementations=2.verified.txt @@ -0,0 +1,103 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..ce603215d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Long_implementations=4.verified.txt @@ -0,0 +1,104 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..e22824662 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=Long_implementations=6.verified.txt @@ -0,0 +1,104 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..71395a96d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=MassTransitNewId_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..5fb326f84 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=MassTransitNewId_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..cee9dd38e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=MassTransitNewId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..0141a3593 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=MassTransitNewId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..38f0dc630 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=NullableString_implementations=0.verified.txt @@ -0,0 +1,131 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..46931452c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=NullableString_implementations=2.verified.txt @@ -0,0 +1,131 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..ec5328185 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=NullableString_implementations=4.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..b172dff7d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=NullableString_implementations=6.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..d610b0625 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,131 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..0b1ad79f0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,131 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..7c0b3a847 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,132 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..319264c3a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,132 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..3542ac44b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=String_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..0e6f405af --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=String_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..ad7870050 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=String_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..cc0631b54 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=String_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..843f02da5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,101 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..a901995e9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,101 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..05ed6addb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,102 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..88bb91749 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,102 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..bf24a7693 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Guid_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..1b7e8a5f9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Guid_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..ad859b73a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Guid_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..b1eba0307 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Guid_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..12d95135f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Int_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..7b05a8bae --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Int_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..f4326ac17 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..261f1f5ae --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..e5705f7db --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Long_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..a83106eb9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Long_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..4c950d5d2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Long_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..ecdfce7d5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=Long_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..53d8b1503 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=MassTransitNewId_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..9328f64fa --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=MassTransitNewId_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..f3ab733c6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=MassTransitNewId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..9a0af0230 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=MassTransitNewId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..846e7ba29 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=NullableString_implementations=0.verified.txt @@ -0,0 +1,174 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..4f0f1f82e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=NullableString_implementations=2.verified.txt @@ -0,0 +1,174 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..5faeea43b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=NullableString_implementations=4.verified.txt @@ -0,0 +1,184 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..54a3fb143 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=NullableString_implementations=6.verified.txt @@ -0,0 +1,184 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..571f1903e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,174 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..e04710ed2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,174 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..83312b51a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,175 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..333647949 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,175 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..bac58331f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=String_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..9c0b7a7ad --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=String_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..6de7fe568 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=String_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..1be61068b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=String_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..efed92064 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Guid_implementations=0.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..f07104b64 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Guid_implementations=2.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..bb7fcccb6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Guid_implementations=4.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..b9cc23803 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Guid_implementations=6.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..9e74b58bc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Int_implementations=0.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..5458bb936 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Int_implementations=2.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..e9d5b5353 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Int_implementations=4.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..4db0b795f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Int_implementations=6.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..da4b279df --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Long_implementations=0.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..44932ed29 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Long_implementations=2.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..79279f1d8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Long_implementations=4.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..190aa6c33 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=Long_implementations=6.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..be82a5180 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=MassTransitNewId_implementations=0.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..394dc1de3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=MassTransitNewId_implementations=2.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..ab2adff95 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=MassTransitNewId_implementations=4.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..ad03bfb65 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=MassTransitNewId_implementations=6.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..c9dd81a18 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=NullableString_implementations=0.verified.txt @@ -0,0 +1,94 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..f6da20618 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=NullableString_implementations=2.verified.txt @@ -0,0 +1,94 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..421f62eef --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=NullableString_implementations=4.verified.txt @@ -0,0 +1,104 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..7c633e569 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=NullableString_implementations=6.verified.txt @@ -0,0 +1,104 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..41aedf49a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=0.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..6d6f396fd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=2.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..38b28b8df --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=4.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..96e7d6255 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=6.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..c39346e75 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=String_implementations=0.verified.txt @@ -0,0 +1,85 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..4e840f400 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=String_implementations=2.verified.txt @@ -0,0 +1,85 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..6f837c9bf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=String_implementations=4.verified.txt @@ -0,0 +1,95 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..f94c6433d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=String_implementations=6.verified.txt @@ -0,0 +1,95 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..33da0b6cb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Guid_implementations=0.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..9bb869a2f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Guid_implementations=2.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..925d0863f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Guid_implementations=4.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..074560580 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Guid_implementations=6.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..f2ff27cfd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Int_implementations=0.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..62a6d461d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Int_implementations=2.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..9bf84c59b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Int_implementations=4.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..318c92cd9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Int_implementations=6.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..eebd5a877 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Long_implementations=0.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..7291aa0ef --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Long_implementations=2.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..4ec1982b6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Long_implementations=4.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..daeca6fd9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=Long_implementations=6.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..d87589431 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=MassTransitNewId_implementations=0.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..c53aa99cf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=MassTransitNewId_implementations=2.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..f7c4d7c88 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=MassTransitNewId_implementations=4.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..b86d1ea6e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=MassTransitNewId_implementations=6.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..5c7579246 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=NullableString_implementations=0.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 + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..117e34145 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=NullableString_implementations=2.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 + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..1b84dd689 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=NullableString_implementations=4.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 + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..877cf3895 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=NullableString_implementations=6.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 + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..9d3904768 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,136 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..f675ba5d1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,136 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..90e3475b7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,137 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..1d88fd487 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,137 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..87f60370a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=String_implementations=0.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..94cde993d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=String_implementations=2.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..ea482a605 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=String_implementations=4.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..8395a15bb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=String_implementations=6.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..6945a97e9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Guid_implementations=0.verified.txt @@ -0,0 +1,99 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..416a54b37 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Guid_implementations=2.verified.txt @@ -0,0 +1,99 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..142e9e8bc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Guid_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..f9ce54635 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Guid_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..9da312b40 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Int_implementations=0.verified.txt @@ -0,0 +1,99 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..0cbae799a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Int_implementations=2.verified.txt @@ -0,0 +1,99 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..80bc2ad23 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..e05b51f1a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..dcbe8bfd8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Long_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..a14d4b352 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Long_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..1791ec98a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Long_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..9fa264a9e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=Long_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..b39a469d3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=MassTransitNewId_implementations=0.verified.txt @@ -0,0 +1,99 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..ffccbb586 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=MassTransitNewId_implementations=2.verified.txt @@ -0,0 +1,99 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..83b3a99b7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=MassTransitNewId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..7e47251aa --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=MassTransitNewId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..3fd87c103 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=NullableString_implementations=0.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 + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..cd1b457de --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=NullableString_implementations=2.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 + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..8ef875ec3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=NullableString_implementations=4.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 + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..32a4e6939 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=NullableString_implementations=6.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 + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..3144a199c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..bb50b2dfd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..6ce7f9019 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..f94f316f0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..d666b57cb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=String_implementations=0.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..bc3fa21a7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=String_implementations=2.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..2ea4c765e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=String_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..3909c1ce1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=String_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..e19aa8e97 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Guid_implementations=0.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..91967bb1a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Guid_implementations=2.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..40392916b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Guid_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..113ce1d88 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Guid_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..e520cc07a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Int_implementations=0.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..fd58e807f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Int_implementations=2.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..86c2e4e16 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..d4898e00b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..d67bd6566 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Long_implementations=0.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..43b592af5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Long_implementations=2.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..02a36723c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Long_implementations=4.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..63f45a7f3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=Long_implementations=6.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..756f7ecfc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=MassTransitNewId_implementations=0.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..1645a01f1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=MassTransitNewId_implementations=2.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..71a5f20df --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=MassTransitNewId_implementations=4.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..1437926c3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=MassTransitNewId_implementations=6.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..36cf61ba8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=NullableString_implementations=0.verified.txt @@ -0,0 +1,164 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..ec4662112 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=NullableString_implementations=2.verified.txt @@ -0,0 +1,164 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..331a158f9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=NullableString_implementations=4.verified.txt @@ -0,0 +1,174 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..d1e37b854 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=NullableString_implementations=6.verified.txt @@ -0,0 +1,174 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..045dd5aab --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,163 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..8f6299206 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,163 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..7d909309a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,164 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..445ce0667 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,164 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..b56894653 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=String_implementations=0.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..2e40c823a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=String_implementations=2.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..268038259 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=String_implementations=4.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..c1034e002 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=String_implementations=6.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..b21354ae1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Guid_implementations=0.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..be4d1742f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Guid_implementations=2.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..deaa6ec83 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Guid_implementations=4.verified.txt @@ -0,0 +1,93 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..9ab6d027f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Guid_implementations=6.verified.txt @@ -0,0 +1,93 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..4ebc676c7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Int_implementations=0.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..99d4d17bd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Int_implementations=2.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..8aeb6cb5c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Int_implementations=4.verified.txt @@ -0,0 +1,93 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..bf31da1b6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Int_implementations=6.verified.txt @@ -0,0 +1,93 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..64c410f96 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Long_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..12c692c70 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Long_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..05977d874 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Long_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..308704577 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=Long_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..d3a51fb90 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=MassTransitNewId_implementations=0.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..e6c81a32b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=MassTransitNewId_implementations=2.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..9d6d1d1e0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=MassTransitNewId_implementations=4.verified.txt @@ -0,0 +1,93 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..388921a0b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=MassTransitNewId_implementations=6.verified.txt @@ -0,0 +1,93 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..033d2a07d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=NullableString_implementations=0.verified.txt @@ -0,0 +1,115 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..29764be0d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=NullableString_implementations=2.verified.txt @@ -0,0 +1,115 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..6cb5b0e35 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=NullableString_implementations=4.verified.txt @@ -0,0 +1,125 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..d48030990 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=NullableString_implementations=6.verified.txt @@ -0,0 +1,125 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..abd8feb10 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,115 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..94b712b1e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,115 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..2ccd1532d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,116 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..32ae9eb7b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,116 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..72f0f90b5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=String_implementations=0.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..4c93ed6dc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=String_implementations=2.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..446d0af58 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=String_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..19ba1c79c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=String_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..fd41d441f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Guid_implementations=0.verified.txt @@ -0,0 +1,134 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..8e0e8bd35 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Guid_implementations=2.verified.txt @@ -0,0 +1,134 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..9f099be17 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Guid_implementations=4.verified.txt @@ -0,0 +1,135 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..dc379946d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Guid_implementations=6.verified.txt @@ -0,0 +1,135 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..91a04a3cd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Int_implementations=0.verified.txt @@ -0,0 +1,134 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..c0a612658 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Int_implementations=2.verified.txt @@ -0,0 +1,134 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..5e46264f6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Int_implementations=4.verified.txt @@ -0,0 +1,135 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..0f5531b96 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Int_implementations=6.verified.txt @@ -0,0 +1,135 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..8477ea883 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Long_implementations=0.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..0951187da --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Long_implementations=2.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..449521cec --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Long_implementations=4.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..d0d7a82f0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=Long_implementations=6.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..756a6b32d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=MassTransitNewId_implementations=0.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..6b7414e33 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=MassTransitNewId_implementations=2.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..b392596bb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=MassTransitNewId_implementations=4.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..79d36c507 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=MassTransitNewId_implementations=6.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..a09c2afd2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=NullableString_implementations=0.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 + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..50e8b8eec --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=NullableString_implementations=2.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 + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..1d1d53e22 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=NullableString_implementations=4.verified.txt @@ -0,0 +1,168 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..f1b3533d4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=NullableString_implementations=6.verified.txt @@ -0,0 +1,168 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..34edcfbc0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=0.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..f5707e28d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=2.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..7d772411a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=4.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..a20e4d1d9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=6.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..4d43a0dfd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=String_implementations=0.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..637d3de30 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=String_implementations=2.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..ed61c154d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=String_implementations=4.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..95d395419 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=String_implementations=6.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..5a946bf37 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Guid_implementations=0.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..c5d84dd0c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Guid_implementations=2.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..9681b3692 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Guid_implementations=4.verified.txt @@ -0,0 +1,114 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..540c235d8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Guid_implementations=6.verified.txt @@ -0,0 +1,114 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..2af007dd3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Int_implementations=0.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..8a3456051 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Int_implementations=2.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..d273e6acb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Int_implementations=4.verified.txt @@ -0,0 +1,114 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..5242cf9d7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Int_implementations=6.verified.txt @@ -0,0 +1,114 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..af73ab19b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Long_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..86ca66f54 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Long_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..1eae39932 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Long_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..e48c8a4d9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=Long_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..7e4c2d74f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=MassTransitNewId_implementations=0.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..a6f79fc62 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=MassTransitNewId_implementations=2.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..cf1b99adf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=MassTransitNewId_implementations=4.verified.txt @@ -0,0 +1,114 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..0d5ed4beb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=MassTransitNewId_implementations=6.verified.txt @@ -0,0 +1,114 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..d3bba8492 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=NullableString_implementations=0.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 + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..79f35fb86 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=NullableString_implementations=2.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 + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..0a7d7ec62 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=NullableString_implementations=4.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..aea2c28a2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=NullableString_implementations=6.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..931efc347 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..44dd298a4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..a0ac681d7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=4.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..2c1e4b81e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=6.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..607a634fa --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=String_implementations=0.verified.txt @@ -0,0 +1,119 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..eac7ef83f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=String_implementations=2.verified.txt @@ -0,0 +1,119 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..10d80394d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=String_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..0ae95dad1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=String_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..2e7c7382a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Guid_implementations=0.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..d64c74c0d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Guid_implementations=2.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..3caf56ad3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Guid_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..16e6671eb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Guid_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..c7026e842 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Int_implementations=0.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..cf4e510ce --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Int_implementations=2.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..f085f97e9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..ad8b27c58 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..e93a16d01 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Long_implementations=0.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..f00fb42a2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Long_implementations=2.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..461536e04 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Long_implementations=4.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..6e1ff6f67 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=Long_implementations=6.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..ec1d660b5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=MassTransitNewId_implementations=0.verified.txt @@ -0,0 +1,163 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..a0e99e8be --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=MassTransitNewId_implementations=2.verified.txt @@ -0,0 +1,163 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..84eb9567c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=MassTransitNewId_implementations=4.verified.txt @@ -0,0 +1,164 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..dece95dc8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=MassTransitNewId_implementations=6.verified.txt @@ -0,0 +1,164 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..b2b2a5ee3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=NullableString_implementations=0.verified.txt @@ -0,0 +1,185 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..958f38bda --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=NullableString_implementations=2.verified.txt @@ -0,0 +1,185 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..c4e1b8491 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=NullableString_implementations=4.verified.txt @@ -0,0 +1,195 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..607e5978e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=NullableString_implementations=6.verified.txt @@ -0,0 +1,195 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..1b69ebc7d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,185 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..1d1c030d3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,185 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..7fdc59ae9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,186 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..37a385fac --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,186 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..0b061cf3e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=String_implementations=0.verified.txt @@ -0,0 +1,157 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..9108c4072 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=String_implementations=2.verified.txt @@ -0,0 +1,157 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..86a1dda8b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=String_implementations=4.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..404e024dd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=String_implementations=6.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..2621e45e2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,85 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..13d5e3318 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,85 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..65f6776e0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,86 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..c090468cb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,86 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..0d4c9b11e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=0.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 + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..82bc98e5b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=2.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.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..0aadeaa16 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=4.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.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..178119bc0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=6.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.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..ba23bc1a2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,47 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..51523c90b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,47 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..5b8b8b970 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,48 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..2302d348d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,48 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..6e7c69adb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=0.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 MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..c277d28d6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=2.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 MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..eabf7fd63 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=4.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 MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..fc71d3a78 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=6.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 MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..72a6b7846 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=0.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))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..5c2453cf7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=2.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))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..010dd1362 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=4.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..000f16d85 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=6.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..e6ca63341 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=0.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..beed85030 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=2.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..57a5e7aa4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=4.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..28526dddd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=6.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..df4d31b6d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=0.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..3ef3a8e63 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=2.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..f2dee4335 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=4.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 MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..d1e2c5ee3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=6.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 MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..659a37838 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,112 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..ffe832d5f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,112 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..00bb4b549 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..2f648a037 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..ec2515165 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=0.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..22ad494ee --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=2.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..b2b28fe2b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=4.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.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..e028b8fc0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=6.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.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..87adcaf74 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=ObjectId_implementations=0.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 MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..aadbddf17 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=ObjectId_implementations=2.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 MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..2ac70d923 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=ObjectId_implementations=4.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 MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..7ca8f621a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=ObjectId_implementations=6.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 MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..75e46cb5e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=0.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 + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..9ad852aab --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=2.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.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..f941793ff --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=4.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..4a168987f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=6.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 + + [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 MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..84eb1273a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=0.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 + + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..f7fc1267b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=2.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 + + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..dbf784590 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=4.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 + + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..e09cff614 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=6.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 + + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..aad89de0e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=0.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..4fac08c3c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=2.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 + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..49a78c4de --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=4.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.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..7c6e818d6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=6.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.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..143409604 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=0.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..5c2e808f9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=2.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..6645dd7d3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,82 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..5df3fc3bd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,82 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..85f156a5c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,124 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..f605f8b30 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,124 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..4adc27f81 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,125 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..781f833aa --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,125 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..0ce301beb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=0.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..ea209d846 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=2.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..b41b6c0a6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=4.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.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..0b651168c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=6.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.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..8d6384329 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,119 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..9d6764288 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,119 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..db6748745 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,120 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..bdfc20ca6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,120 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..986322f52 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,103 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..3e9a14f55 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,103 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..026157252 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,104 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..ba0502002 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,104 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..e23b13455 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=0.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 MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..01fd13f33 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=2.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 MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..e6b71d400 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=4.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.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..79856c3b4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=6.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.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..3720d6d78 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=0.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 + + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..c64aa3394 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=2.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 + + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..c9fcf1fb5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=4.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 + + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..09451a98e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=6.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 + + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..4ab2d843c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=0.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..0043961dd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=2.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..76b8ef930 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=4.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..6b7513235 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=6.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..2218d209e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=0.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..c881adbc9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=2.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..725b8abf6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=4.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.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..042104e32 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=6.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.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..1dce32451 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..a2b0658cb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..594365f90 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,93 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..ac67d3102 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,93 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..8c9bf07a4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=0.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..a28cd1170 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=2.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..effe0fa4b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=4.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..d20e75c76 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=6.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..9de1d9892 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=0.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 + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..d2d4214fc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=2.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.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..7a286ca44 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=4.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.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..aab7999ad --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=6.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.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..8271ac1f5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=0.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..9418d2304 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=2.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..4ec635df4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=4.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.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..c4e2be596 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=6.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.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..d67f18b4a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=0.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))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..61b0ec9d5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=2.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))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..b2e94c03e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=4.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))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..3048f67ab --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=6.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))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..bcaeec478 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,157 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#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 MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..4dff83c9f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,157 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#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 MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..14aabde7f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=4.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..74f46cdb1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=6.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 + + [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 MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..ab25ad658 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Guid_implementations=0.verified.txt @@ -0,0 +1,49 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..c22f190b7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Guid_implementations=2.verified.txt @@ -0,0 +1,49 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..2fae94c15 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Guid_implementations=4.verified.txt @@ -0,0 +1,50 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..075195897 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Guid_implementations=6.verified.txt @@ -0,0 +1,50 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..5cf56892b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Int_implementations=0.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..7cfaa692f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Int_implementations=2.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..34ed4a43b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Int_implementations=4.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..901f0f012 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Int_implementations=6.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..a6a0a222f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Long_implementations=0.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..c03a8d013 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Long_implementations=2.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..ae758f60a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Long_implementations=4.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..eba61f6bb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=Long_implementations=6.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..8e4e560d2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=MassTransitNewId_implementations=0.verified.txt @@ -0,0 +1,49 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..6fe63c7e1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=MassTransitNewId_implementations=2.verified.txt @@ -0,0 +1,49 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..1ec341f47 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=MassTransitNewId_implementations=4.verified.txt @@ -0,0 +1,50 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..b5144cae1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=MassTransitNewId_implementations=6.verified.txt @@ -0,0 +1,50 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..bd7a38e8f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=NullableString_implementations=0.verified.txt @@ -0,0 +1,64 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..9e47cab82 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=NullableString_implementations=2.verified.txt @@ -0,0 +1,64 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..229b7ba3e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=NullableString_implementations=4.verified.txt @@ -0,0 +1,74 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..8775af5e3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=NullableString_implementations=6.verified.txt @@ -0,0 +1,74 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..565f692ae --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,64 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..b2744c085 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,64 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..a393bbe8a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,65 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..da0d8c3af --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,65 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..888172c13 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=String_implementations=0.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..a572e42a7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=String_implementations=2.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..2257e1eec --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=String_implementations=4.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..7df51bcbe --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=String_implementations=6.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..3aea884b9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Guid_implementations=0.verified.txt @@ -0,0 +1,91 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..61749dd5b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Guid_implementations=2.verified.txt @@ -0,0 +1,91 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..5b4385c8c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Guid_implementations=4.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..8267402f9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Guid_implementations=6.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..792056fa6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Int_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..0c23b39cc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Int_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..2398479da --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..9cd0c4692 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..3a2cc7650 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Long_implementations=0.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..dfda29d40 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Long_implementations=2.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..b282ca953 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Long_implementations=4.verified.txt @@ -0,0 +1,93 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..1cf8f6301 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=Long_implementations=6.verified.txt @@ -0,0 +1,93 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..d706addbf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=MassTransitNewId_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..e37ec0418 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=MassTransitNewId_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..422ed97a8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=MassTransitNewId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..dc89657c9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=MassTransitNewId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..ffbc6498f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=NullableString_implementations=0.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..b2c588715 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=NullableString_implementations=2.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..6a4e0a03e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=NullableString_implementations=4.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 + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..812603ba0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=NullableString_implementations=6.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 + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..23539c3d9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..eaf6e07b3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..2c01c313a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=4.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..57d200982 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=6.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..e234ec82a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=String_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..cabec6b0b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=String_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..6c81b9961 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=String_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..7dd5971f5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=String_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..1c0300c30 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Guid_implementations=0.verified.txt @@ -0,0 +1,70 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..b81674041 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Guid_implementations=2.verified.txt @@ -0,0 +1,70 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..77e317e52 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Guid_implementations=4.verified.txt @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..91898352c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Guid_implementations=6.verified.txt @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..a4994705d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Int_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..2f7364cb6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Int_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..07d69651d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..db8c2efa7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..8fb26733a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Long_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..252ddb03c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Long_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..cc29011a4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Long_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..cd8b91da4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=Long_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..b58d65d0b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=MassTransitNewId_implementations=0.verified.txt @@ -0,0 +1,70 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..1350b3bd1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=MassTransitNewId_implementations=2.verified.txt @@ -0,0 +1,70 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..7ec167ded --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=MassTransitNewId_implementations=4.verified.txt @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..b6eceb384 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=MassTransitNewId_implementations=6.verified.txt @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..8256a84fb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=NullableString_implementations=0.verified.txt @@ -0,0 +1,91 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..4fad098a6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=NullableString_implementations=2.verified.txt @@ -0,0 +1,91 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..c6868a819 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=NullableString_implementations=4.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..93a86d0ff --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=NullableString_implementations=6.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..73837ad61 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,91 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..063f33a17 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,91 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..3491b714b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..685735df9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..8559b12bd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=String_implementations=0.verified.txt @@ -0,0 +1,77 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..b03c15a54 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=String_implementations=2.verified.txt @@ -0,0 +1,77 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..64a4805a3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=String_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..6a0997619 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=String_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..9954a512d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,106 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..e28e9a710 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,106 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..a763f7a43 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,107 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..aecd6c4ba --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,107 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..32a7ca05c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Guid_implementations=0.verified.txt @@ -0,0 +1,112 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..8d7c3af2a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Guid_implementations=2.verified.txt @@ -0,0 +1,112 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..849338511 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Guid_implementations=4.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..405d5651f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Guid_implementations=6.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..1d0aca659 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Int_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..dee2209bf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Int_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..c1dffa507 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..abf8fc683 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..f4566cb21 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Long_implementations=0.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..504a47540 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Long_implementations=2.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..ed2124ae0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Long_implementations=4.verified.txt @@ -0,0 +1,114 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..01c136e89 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=Long_implementations=6.verified.txt @@ -0,0 +1,114 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..de31619cf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=MassTransitNewId_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..9183fc825 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=MassTransitNewId_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..09cecf30e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=MassTransitNewId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..356991a0d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=MassTransitNewId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..b51e41ab0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=NullableString_implementations=0.verified.txt @@ -0,0 +1,134 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..2c29357cf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=NullableString_implementations=2.verified.txt @@ -0,0 +1,134 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..a0bcd71ab --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=NullableString_implementations=4.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..36f572e99 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=NullableString_implementations=6.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..25c2a5266 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,134 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..420220cb9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,134 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..43058e788 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,135 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..c0ffb59d1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,135 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..33ed7376a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=String_implementations=0.verified.txt @@ -0,0 +1,115 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..ae05f7004 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=String_implementations=2.verified.txt @@ -0,0 +1,115 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..bedea3b36 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=String_implementations=4.verified.txt @@ -0,0 +1,125 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..0081e805a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=String_implementations=6.verified.txt @@ -0,0 +1,125 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..c4419a87f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Guid_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..1f5f84c1c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Guid_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..14dd1b523 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Guid_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..b26dd7441 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Guid_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..6b071674a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Int_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..ae852241a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Int_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..424c0a70c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..c7bc9ff83 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..3712df576 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Long_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..87350a75d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Long_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..baf94531b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Long_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..bb44732bd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=Long_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..2ed2be111 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=MassTransitNewId_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..6a548e864 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=MassTransitNewId_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..7b563f31f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=MassTransitNewId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..a66d49929 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=MassTransitNewId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..5b7f85692 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=NullableString_implementations=0.verified.txt @@ -0,0 +1,85 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..a9128cb4f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=NullableString_implementations=2.verified.txt @@ -0,0 +1,85 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..9759d8731 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=NullableString_implementations=4.verified.txt @@ -0,0 +1,95 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..6fa6d3ae3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=NullableString_implementations=6.verified.txt @@ -0,0 +1,95 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..e7a289d90 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=0.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..bb1090982 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=2.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..465ed5d1f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..0a7cdb2d0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..d9e32f8b9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=String_implementations=0.verified.txt @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..35af7c107 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=String_implementations=2.verified.txt @@ -0,0 +1,71 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..c79ee78d8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=String_implementations=4.verified.txt @@ -0,0 +1,81 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..9f1071ba4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=String_implementations=6.verified.txt @@ -0,0 +1,81 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..138ea6e67 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Guid_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..c69474921 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Guid_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..9c0bb300b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Guid_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..5577030b9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Guid_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..2b321a3b5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Int_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..61aa54592 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Int_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..d147ffd3e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..4c2b4a1cb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..143dbd0c4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Long_implementations=0.verified.txt @@ -0,0 +1,106 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..097067cf9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Long_implementations=2.verified.txt @@ -0,0 +1,106 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..a69641ac9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Long_implementations=4.verified.txt @@ -0,0 +1,107 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..e426b5359 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=Long_implementations=6.verified.txt @@ -0,0 +1,107 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..2ec234cd4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=MassTransitNewId_implementations=0.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..5ba24d23e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=MassTransitNewId_implementations=2.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..8867398b8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=MassTransitNewId_implementations=4.verified.txt @@ -0,0 +1,114 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..1a6fba5f4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=MassTransitNewId_implementations=6.verified.txt @@ -0,0 +1,114 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..6af8a3fba --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=NullableString_implementations=0.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 + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..0fd4a0e58 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=NullableString_implementations=2.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 + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..fe385afb2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=NullableString_implementations=4.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 + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..39b624a24 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=NullableString_implementations=6.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 + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..7291d9ed7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=0.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..19501dc98 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=2.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..75fa39373 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=4.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..a857313cd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=6.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..3179023e1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=String_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..88f461aa6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=String_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..f5f54975f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=String_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..5248c55c8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=String_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..d0b87c211 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Guid_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..36790b2fe --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Guid_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..9baa0c647 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Guid_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..deaa4c8ea --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Guid_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..b0e7bae66 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Int_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..1cc28f6ba --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Int_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..29cafc740 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..95544b9b9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..a6958c4a2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Long_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..5dae589e0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Long_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..745cdeeb2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Long_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..0996d4b68 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=Long_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value); + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return result.HasValue ? new MyTestId(result.Value) : null; + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..39f6f0c66 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=MassTransitNewId_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..a4de18636 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=MassTransitNewId_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..983d03c3e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=MassTransitNewId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..402bc86a7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=MassTransitNewId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..2f01714d6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=NullableString_implementations=0.verified.txt @@ -0,0 +1,112 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..e0b1c4c22 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=NullableString_implementations=2.verified.txt @@ -0,0 +1,112 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..8aa7d60ce --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=NullableString_implementations=4.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 + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..02a406aa9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=NullableString_implementations=6.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 + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..16f297244 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..6d398eba5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..254d2b0ed --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,114 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..15f5ae03f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,114 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..ed9441fa6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=String_implementations=0.verified.txt @@ -0,0 +1,91 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..4f00813a4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=String_implementations=2.verified.txt @@ -0,0 +1,91 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..b47f58c3e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=String_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..94892a555 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=String_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(MyTestId); + } + + public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) + { + var id = (MyTestId)value; + serializer.Serialize(writer, id.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..98cca7f88 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Guid_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..4b1ff49bb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Guid_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..fc2e03d8a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Guid_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..f79f1d81b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Guid_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..17402a3d0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Int_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..88663248e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Int_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..77858fcd5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..e476903c9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..f0ed2b7c1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Long_implementations=0.verified.txt @@ -0,0 +1,127 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..93b910eb2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Long_implementations=2.verified.txt @@ -0,0 +1,127 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..2fecb575d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Long_implementations=4.verified.txt @@ -0,0 +1,128 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..24f96bb2a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=Long_implementations=6.verified.txt @@ -0,0 +1,128 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..22f5ed36f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=MassTransitNewId_implementations=0.verified.txt @@ -0,0 +1,134 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..eb62376d2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=MassTransitNewId_implementations=2.verified.txt @@ -0,0 +1,134 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..9edda00a8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=MassTransitNewId_implementations=4.verified.txt @@ -0,0 +1,135 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..ec53924de --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=MassTransitNewId_implementations=6.verified.txt @@ -0,0 +1,135 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..624d8eb64 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=NullableString_implementations=0.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..3443ff756 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=NullableString_implementations=2.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..712789ea7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=NullableString_implementations=4.verified.txt @@ -0,0 +1,165 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..47df415cd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=NullableString_implementations=6.verified.txt @@ -0,0 +1,165 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext? context, System.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..b5ed196f5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..0de2b1993 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..b8a6a0b07 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,157 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..1cd6f80db --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,157 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..48f162817 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=String_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..1c641d9f7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=String_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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); + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..a1c0ec20f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=String_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..313c2f1b2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=String_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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), + }; + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..6e562f502 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Guid_implementations=0.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..2f703f944 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Guid_implementations=2.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..14aeb8dda --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Guid_implementations=4.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..625c68ced --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Guid_implementations=6.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..4952c60e6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Int_implementations=0.verified.txt @@ -0,0 +1,59 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..d52990e48 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Int_implementations=2.verified.txt @@ -0,0 +1,59 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..47a64e3b8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Int_implementations=4.verified.txt @@ -0,0 +1,60 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..f76adefb7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Int_implementations=6.verified.txt @@ -0,0 +1,60 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..c395a9205 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Long_implementations=0.verified.txt @@ -0,0 +1,59 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..2e066c49a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Long_implementations=2.verified.txt @@ -0,0 +1,59 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..0e9e3eb38 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Long_implementations=4.verified.txt @@ -0,0 +1,60 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..a30095618 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=Long_implementations=6.verified.txt @@ -0,0 +1,60 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..6cd06c343 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=MassTransitNewId_implementations=0.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..cd1153c06 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=MassTransitNewId_implementations=2.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..bd7aa60ba --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=MassTransitNewId_implementations=4.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..ca56c2c55 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=MassTransitNewId_implementations=6.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..f6b884ae6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=NullableString_implementations=0.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 + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..a22832ff9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=NullableString_implementations=2.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 + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..252ad9492 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=NullableString_implementations=4.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 + +#nullable enable + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..8402a8cee --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=NullableString_implementations=6.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 + +#nullable enable + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..7a294ed54 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=0.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..bdff64e42 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=2.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..794ce6e6f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=4.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..170a7a62b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=6.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..61ae49313 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=String_implementations=0.verified.txt @@ -0,0 +1,68 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..e90f5b8cb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=String_implementations=2.verified.txt @@ -0,0 +1,68 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..d698d2a97 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=String_implementations=4.verified.txt @@ -0,0 +1,78 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..63519be9e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=String_implementations=6.verified.txt @@ -0,0 +1,78 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + value => new MyTestId(value), + mappingHints + ) { } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..6fde1ea2e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Guid_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..77e9a4cc7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Guid_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..8947aaaf6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Guid_implementations=4.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..0b2cb9803 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Guid_implementations=6.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..ea47e0290 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Int_implementations=0.verified.txt @@ -0,0 +1,101 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..4980a7f75 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Int_implementations=2.verified.txt @@ -0,0 +1,101 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..1625c9487 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Int_implementations=4.verified.txt @@ -0,0 +1,102 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..aad7520e0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Int_implementations=6.verified.txt @@ -0,0 +1,102 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..e09d3ac6f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Long_implementations=0.verified.txt @@ -0,0 +1,103 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..68afdc4eb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Long_implementations=2.verified.txt @@ -0,0 +1,103 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..2dea6e57d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Long_implementations=4.verified.txt @@ -0,0 +1,104 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..b4f470ff3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=Long_implementations=6.verified.txt @@ -0,0 +1,104 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..a50b29456 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=MassTransitNewId_implementations=0.verified.txt @@ -0,0 +1,110 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..ba136d53d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=MassTransitNewId_implementations=2.verified.txt @@ -0,0 +1,110 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..f38c2686a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=MassTransitNewId_implementations=4.verified.txt @@ -0,0 +1,111 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..317eed93c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=MassTransitNewId_implementations=6.verified.txt @@ -0,0 +1,111 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..0356a7fae --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=NullableString_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..2f9bd206b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=NullableString_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..9cd540df5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=NullableString_implementations=4.verified.txt @@ -0,0 +1,128 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..02094a269 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=NullableString_implementations=6.verified.txt @@ -0,0 +1,128 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..b86265af8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..bc22d3d58 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..0b7a5c3af --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=4.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..528d4c265 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=6.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..9af9c71f2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=String_implementations=0.verified.txt @@ -0,0 +1,106 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..ad5c96a18 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=String_implementations=2.verified.txt @@ -0,0 +1,106 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..68eada8bb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=String_implementations=4.verified.txt @@ -0,0 +1,116 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..f517b4514 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=String_implementations=6.verified.txt @@ -0,0 +1,116 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..a38f053bd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Guid_implementations=0.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..fbefc3661 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Guid_implementations=2.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..b60258908 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Guid_implementations=4.verified.txt @@ -0,0 +1,82 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..cf70988e7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Guid_implementations=6.verified.txt @@ -0,0 +1,82 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..3972c65d6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Int_implementations=0.verified.txt @@ -0,0 +1,80 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..88c12d9ed --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Int_implementations=2.verified.txt @@ -0,0 +1,80 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..e10be3b00 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Int_implementations=4.verified.txt @@ -0,0 +1,81 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..2e60e768b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Int_implementations=6.verified.txt @@ -0,0 +1,81 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..7ce27b1e2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Long_implementations=0.verified.txt @@ -0,0 +1,80 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..7b2fa7e99 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Long_implementations=2.verified.txt @@ -0,0 +1,80 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..e6210fa41 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Long_implementations=4.verified.txt @@ -0,0 +1,81 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..ce60b80fb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=Long_implementations=6.verified.txt @@ -0,0 +1,81 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..4a6a143c4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=MassTransitNewId_implementations=0.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..d7b63021d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=MassTransitNewId_implementations=2.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..312514722 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=MassTransitNewId_implementations=4.verified.txt @@ -0,0 +1,82 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..1ccb3530a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=MassTransitNewId_implementations=6.verified.txt @@ -0,0 +1,82 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..d8091c267 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=NullableString_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..1fb888167 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=NullableString_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..91c9f4af5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=NullableString_implementations=4.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..17c25ac87 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=NullableString_implementations=6.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..5eb341e75 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..b5b818501 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..32791c7ee --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..83ac52244 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..ded55a7f0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=String_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..bccdfda53 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=String_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..354fa90ed --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=String_implementations=4.verified.txt @@ -0,0 +1,98 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..922efe564 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=String_implementations=6.verified.txt @@ -0,0 +1,98 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..1bb32b1e6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Guid_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..2cbb12c64 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Guid_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..7a8220cc7 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Guid_implementations=4.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..530ffaf1b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Guid_implementations=6.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..43a7a947b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Int_implementations=0.verified.txt @@ -0,0 +1,122 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..462be231f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Int_implementations=2.verified.txt @@ -0,0 +1,122 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..a02ed9b3b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Int_implementations=4.verified.txt @@ -0,0 +1,123 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..c881f7464 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Int_implementations=6.verified.txt @@ -0,0 +1,123 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..f7affe14c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Long_implementations=0.verified.txt @@ -0,0 +1,124 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..fd89a69d6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Long_implementations=2.verified.txt @@ -0,0 +1,124 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..a239dc6a4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Long_implementations=4.verified.txt @@ -0,0 +1,125 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..4d68b23ce --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=Long_implementations=6.verified.txt @@ -0,0 +1,125 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..d776e89e3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=MassTransitNewId_implementations=0.verified.txt @@ -0,0 +1,131 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..21c78b383 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=MassTransitNewId_implementations=2.verified.txt @@ -0,0 +1,131 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..b03c6819e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=MassTransitNewId_implementations=4.verified.txt @@ -0,0 +1,132 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..3d662bbb9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=MassTransitNewId_implementations=6.verified.txt @@ -0,0 +1,132 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..7b827d741 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=NullableString_implementations=0.verified.txt @@ -0,0 +1,145 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..7373d238f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=NullableString_implementations=2.verified.txt @@ -0,0 +1,145 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..21987e1b5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=NullableString_implementations=4.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..21c51b6b0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=NullableString_implementations=6.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..e770f5aff --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,145 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..914cac096 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,145 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..d9abbb995 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,146 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..bbc5e2aac --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,146 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..25dd8773c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=String_implementations=0.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..50dfa4f4e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=String_implementations=2.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..57d419b98 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=String_implementations=4.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..30cd95527 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=String_implementations=6.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..bcd63f21a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Guid_implementations=0.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..8f869d976 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Guid_implementations=2.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..bc86b7231 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Guid_implementations=4.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..92210b044 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Guid_implementations=6.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..24a9a337c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Int_implementations=0.verified.txt @@ -0,0 +1,73 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..6edaf1b6e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Int_implementations=2.verified.txt @@ -0,0 +1,73 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..f3b410644 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Int_implementations=4.verified.txt @@ -0,0 +1,74 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..71b017e2c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Int_implementations=6.verified.txt @@ -0,0 +1,74 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..08357dac1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Long_implementations=0.verified.txt @@ -0,0 +1,73 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..7a6683f97 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Long_implementations=2.verified.txt @@ -0,0 +1,73 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..6eca3dfa1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Long_implementations=4.verified.txt @@ -0,0 +1,74 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..dd685555c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=Long_implementations=6.verified.txt @@ -0,0 +1,74 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..2502f590e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=MassTransitNewId_implementations=0.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..4570784e8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=MassTransitNewId_implementations=2.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..058f40f56 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=MassTransitNewId_implementations=4.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..60bf30fd1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=MassTransitNewId_implementations=6.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..fa50b56bc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=NullableString_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..5f588a118 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=NullableString_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..f8133a38d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=NullableString_implementations=4.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 + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..7f18bfbf5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=NullableString_implementations=6.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 + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..6b26a9f45 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=0.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..0f0c8cb1f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=2.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..4568b2590 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..10e90de86 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..0fedcfce4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=String_implementations=0.verified.txt @@ -0,0 +1,82 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..3761c2b36 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=String_implementations=2.verified.txt @@ -0,0 +1,82 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..0658b8fcd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=String_implementations=4.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..2be8a6f52 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=String_implementations=6.verified.txt @@ -0,0 +1,92 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..47d5235a4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=0.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..64b586bf9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=2.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..6bf5c77b4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=4.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..974675111 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=6.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..bfb94b06d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Guid_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..6f9be4d88 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Guid_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..953174e19 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Guid_implementations=4.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..fd94b2225 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Guid_implementations=6.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..92f3f68e4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Int_implementations=0.verified.txt @@ -0,0 +1,115 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..763fd7799 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Int_implementations=2.verified.txt @@ -0,0 +1,115 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..8b1b3b13c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Int_implementations=4.verified.txt @@ -0,0 +1,116 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..2ca1ae1f1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Int_implementations=6.verified.txt @@ -0,0 +1,116 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..e38d91ee6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Long_implementations=0.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..1636f2657 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Long_implementations=2.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..d2191d46d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Long_implementations=4.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..ebfe4cbcc --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=Long_implementations=6.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..f44481489 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=MassTransitNewId_implementations=0.verified.txt @@ -0,0 +1,124 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..b0c052505 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=MassTransitNewId_implementations=2.verified.txt @@ -0,0 +1,124 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..ab3a6fee9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=MassTransitNewId_implementations=4.verified.txt @@ -0,0 +1,125 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..d33ca7329 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=MassTransitNewId_implementations=6.verified.txt @@ -0,0 +1,125 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..18f57c0e9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=NullableString_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + 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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..77fee52d5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=NullableString_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + 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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..f1a3c865f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=NullableString_implementations=4.verified.txt @@ -0,0 +1,149 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + 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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..705ea7b2d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=NullableString_implementations=6.verified.txt @@ -0,0 +1,149 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + 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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..1435fe725 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=0.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..5561b8133 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=2.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..616f59560 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=4.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..56669267d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=6.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + 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) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..957916b25 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=String_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..02c5302a0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=String_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..5b421a9bb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=String_implementations=4.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..e24321574 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=String_implementations=6.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 + + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..92f7be021 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Guid_implementations=0.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..8b603016d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Guid_implementations=2.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..eec78753b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Guid_implementations=4.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..195480319 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Guid_implementations=6.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..48d133143 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Int_implementations=0.verified.txt @@ -0,0 +1,94 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..5748e79cf --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Int_implementations=2.verified.txt @@ -0,0 +1,94 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..b72da4133 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Int_implementations=4.verified.txt @@ -0,0 +1,95 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..9ecafa5d0 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Int_implementations=6.verified.txt @@ -0,0 +1,95 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..ca5e51951 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Long_implementations=0.verified.txt @@ -0,0 +1,94 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..b8e92c683 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Long_implementations=2.verified.txt @@ -0,0 +1,94 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..862902917 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Long_implementations=4.verified.txt @@ -0,0 +1,95 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..a041cc36b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=Long_implementations=6.verified.txt @@ -0,0 +1,95 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..0fd5e9310 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=MassTransitNewId_implementations=0.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..1949c3535 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=MassTransitNewId_implementations=2.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..e6a627f67 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=MassTransitNewId_implementations=4.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..46f04ecb6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=MassTransitNewId_implementations=6.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..5c6abceee --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=NullableString_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..740c740be --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=NullableString_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..05351298d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=NullableString_implementations=4.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..083e5d08a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=NullableString_implementations=6.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 + +#nullable enable + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..cbc2fd4b8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,124 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..5681c0435 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,124 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..ffdb06b89 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,125 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..3156f0a48 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,125 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..0f33e44c6 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=String_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..50d086b64 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=String_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..efe73eccb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=String_implementations=4.verified.txt @@ -0,0 +1,112 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..d4c174320 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=String_implementations=6.verified.txt @@ -0,0 +1,112 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..63a2cf996 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Guid_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..9d0c4fae2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Guid_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..cbe8ec3ea --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Guid_implementations=4.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..c6eaa09e4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Guid_implementations=6.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..70e2a60f5 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Int_implementations=0.verified.txt @@ -0,0 +1,136 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..5b49d1056 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Int_implementations=2.verified.txt @@ -0,0 +1,136 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..aa971b191 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Int_implementations=4.verified.txt @@ -0,0 +1,137 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..fc8a02d7c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Int_implementations=6.verified.txt @@ -0,0 +1,137 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..33f494651 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Long_implementations=0.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..42ee24d41 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Long_implementations=2.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.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..827a8a10f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Long_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..10c8f9d40 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=Long_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..3b2e63efd --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=MassTransitNewId_implementations=0.verified.txt @@ -0,0 +1,145 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..b882b0d77 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=MassTransitNewId_implementations=2.verified.txt @@ -0,0 +1,145 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..71faaf990 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=MassTransitNewId_implementations=4.verified.txt @@ -0,0 +1,146 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..d10e6ff6a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=MassTransitNewId_implementations=6.verified.txt @@ -0,0 +1,146 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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()); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..7cadd511d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=NullableString_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + 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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..1c22705a4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=NullableString_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + 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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..ec2d2d379 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=NullableString_implementations=4.verified.txt @@ -0,0 +1,176 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + 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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..d32356c6f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=NullableString_implementations=6.verified.txt @@ -0,0 +1,176 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect 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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints? mappingHints = null) + : base( + id => id.Value!, + 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); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..2c0cfbb5c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=0.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..e9fee261f --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=2.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..7f9a9bc33 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,168 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..741b21505 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,168 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), + value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + mappingHints + ) { } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter + { + public 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 MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) + { + serializer.Serialize(writer, id.Value.ToString()); + } + else + { + serializer.Serialize(writer, null); + } + } + + public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) + { + var result = serializer.Deserialize(reader); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + } + + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter + { + public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) + { + var result = reader.GetString(); + return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + } + + public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + writer.WriteNullValue(); + } + else + { + writer.WriteStringValue(value.Value.ToString()); + } + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..be486bb33 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=String_implementations=0.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..bed5b2865 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=String_implementations=2.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..7f22b48ff --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=String_implementations=4.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..4b53dbb85 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=String_implementations=6.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 + + [Newtonsoft.Json.JsonConverter(typeof(MyTestIdNewtonsoftJsonConverter))] + [System.Text.Json.Serialization.JsonConverter(typeof(MyTestIdSystemTextJsonConverter))] + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter + { + public EfCoreValueConverter() : this(null) { } + public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) + : base( + id => id.Value, + 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); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..18a9d22d2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Guid_implementations=0.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..42387b75c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Guid_implementations=2.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..8438f7e1e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Guid_implementations=4.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..46c15f629 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Guid_implementations=6.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..e8045e4c4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Int_implementations=0.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..5e8b4a915 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Int_implementations=2.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..706bb724b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Int_implementations=4.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..3f9d86bf8 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Int_implementations=6.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..570378aee --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Long_implementations=0.verified.txt @@ -0,0 +1,68 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..47a5f71d4 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Long_implementations=2.verified.txt @@ -0,0 +1,68 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..1dad2545e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Long_implementations=4.verified.txt @@ -0,0 +1,69 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..141880950 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=Long_implementations=6.verified.txt @@ -0,0 +1,69 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..72f753700 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=MassTransitNewId_implementations=0.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..1bc18e221 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=MassTransitNewId_implementations=2.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..87daea683 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=MassTransitNewId_implementations=4.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..2c165b793 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=MassTransitNewId_implementations=6.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..d95dbfe6e --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=NullableString_implementations=0.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 + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..8fefc8469 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=NullableString_implementations=2.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 + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..bb295ac77 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=NullableString_implementations=4.verified.txt @@ -0,0 +1,93 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..777077b0b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=NullableString_implementations=6.verified.txt @@ -0,0 +1,93 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + +#nullable enable + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..84e887c3a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=0.verified.txt @@ -0,0 +1,82 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..407caf1ab --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=2.verified.txt @@ -0,0 +1,82 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..daaad3541 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=4.verified.txt @@ -0,0 +1,83 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..ac9276e58 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=6.verified.txt @@ -0,0 +1,83 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..dcf026251 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=String_implementations=0.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..40d8496d2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=String_implementations=2.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..62188786c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=String_implementations=4.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..e73ad1fc2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=String_implementations=6.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 + + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Guid_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Guid_implementations=0.verified.txt new file mode 100644 index 000000000..904300af1 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Guid_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Guid_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Guid_implementations=2.verified.txt new file mode 100644 index 000000000..4c68383ec --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Guid_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Guid_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Guid_implementations=4.verified.txt new file mode 100644 index 000000000..d354c08f9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Guid_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Guid_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Guid_implementations=6.verified.txt new file mode 100644 index 000000000..81e418b27 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Guid_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new System.Guid(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Int_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Int_implementations=0.verified.txt new file mode 100644 index 000000000..5cd1de0b3 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Int_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Int_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Int_implementations=2.verified.txt new file mode 100644 index 000000000..c6cede945 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Int_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Int_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Int_implementations=4.verified.txt new file mode 100644 index 000000000..5705c8093 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Int_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Int_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Int_implementations=6.verified.txt new file mode 100644 index 000000000..d45b5b5b2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Int_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt32()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt32(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Long_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Long_implementations=0.verified.txt new file mode 100644 index 000000000..6efc58f56 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Long_implementations=0.verified.txt @@ -0,0 +1,112 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Long_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Long_implementations=2.verified.txt new file mode 100644 index 000000000..0c9e23892 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Long_implementations=2.verified.txt @@ -0,0 +1,112 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Long_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Long_implementations=4.verified.txt new file mode 100644 index 000000000..65df9a377 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Long_implementations=4.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Long_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Long_implementations=6.verified.txt new file mode 100644 index 000000000..126ce744c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=Long_implementations=6.verified.txt @@ -0,0 +1,113 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadInt64()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteInt64(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=MassTransitNewId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=MassTransitNewId_implementations=0.verified.txt new file mode 100644 index 000000000..92765a334 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=MassTransitNewId_implementations=0.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=MassTransitNewId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=MassTransitNewId_implementations=2.verified.txt new file mode 100644 index 000000000..fcc5fac5c --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=MassTransitNewId_implementations=2.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=MassTransitNewId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=MassTransitNewId_implementations=4.verified.txt new file mode 100644 index 000000000..92c18269a --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=MassTransitNewId_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=MassTransitNewId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=MassTransitNewId_implementations=6.verified.txt new file mode 100644 index 000000000..5dc592395 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=MassTransitNewId_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(new MassTransit.NewId(context.Reader.ReadString())); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value.ToString()); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=NullableString_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=NullableString_implementations=0.verified.txt new file mode 100644 index 000000000..f90b9ae4d --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=NullableString_implementations=0.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 + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=NullableString_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=NullableString_implementations=2.verified.txt new file mode 100644 index 000000000..5b295fede --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=NullableString_implementations=2.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 + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=NullableString_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=NullableString_implementations=4.verified.txt new file mode 100644 index 000000000..d7726896b --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=NullableString_implementations=4.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 + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=NullableString_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=NullableString_implementations=6.verified.txt new file mode 100644 index 000000000..ae92f19a9 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=NullableString_implementations=6.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 + +#nullable enable + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value is null) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteString(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=0.verified.txt new file mode 100644 index 000000000..e912d9f70 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=0.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=2.verified.txt new file mode 100644 index 000000000..3965e1f81 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=2.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=4.verified.txt new file mode 100644 index 000000000..78d5894be --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=4.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=6.verified.txt new file mode 100644 index 000000000..4bfd7e3be --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=6.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.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + readonly partial struct MyTestId : System.IComparable, System.IEquatable + { + public MongoDB.Bson.ObjectId Value { get; } + + public MyTestId(MongoDB.Bson.ObjectId value) + { + Value = value; + } + + public static MyTestId New() => new MyTestId(MongoDB.Bson.ObjectId.GenerateNewId()); + public static readonly MyTestId Empty = new MyTestId(MongoDB.Bson.ObjectId.Empty); + + public bool Equals(MyTestId other) => this.Value.Equals(other.Value); + public override bool Equals(object obj) + { + if (ReferenceEquals(null, obj)) return false; + return obj is MyTestId other && Equals(other); + } + + public override int GetHashCode() => Value.GetHashCode(); + + public override string ToString() => Value.ToString(); + public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); + public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler + { + public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) + { + parameter.Value = value.Value == MongoDB.Bson.ObjectId.Empty ? null : value.Value.ToString(); + } + + public override MyTestId Parse(object value) + { + return value switch + { + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, + _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), + }; + } + } + + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter + { + public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertFrom + (context, sourceType); + } + + public override object ConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value) + { + return value switch + { + MongoDB.Bson.ObjectId objectIdValue => new MyTestId(objectIdValue), + string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), + _ => base.ConvertFrom(context, culture, value), + }; + } + + public override bool CanConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) + { + return sourceType == typeof(MongoDB.Bson.ObjectId) || sourceType == typeof(string) || base.CanConvertTo(context, sourceType); + } + + public override object ConvertTo(System.ComponentModel.ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, System.Type destinationType) + { + if (value is MyTestId idValue) + { + if (destinationType == typeof(MongoDB.Bson.ObjectId)) + { + return idValue.Value; + } + + if (destinationType == typeof(string)) + { + return idValue.Value.ToString(); + } + } + + return base.ConvertTo(context, culture, value, destinationType); + } + } + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) + { + return new MyTestId(context.Reader.ReadObjectId()); + } + else + { + context.Reader.SkipValue(); + return MyTestId.Empty; + } + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + if (value.Value == MongoDB.Bson.ObjectId.Empty) + { + context.Writer.WriteNull(); + } + else + { + context.Writer.WriteObjectId(value.Value); + } + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=String_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=String_implementations=0.verified.txt new file mode 100644 index 000000000..e32dfd0ad --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=String_implementations=0.verified.txt @@ -0,0 +1,112 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=String_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=String_implementations=2.verified.txt new file mode 100644 index 000000000..8210b6bc2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=String_implementations=2.verified.txt @@ -0,0 +1,112 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by the StronglyTypedId source generator +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +#pragma warning disable 1591 // publicly visible type or member must be documented + + [System.ComponentModel.TypeConverter(typeof(MyTestIdTypeConverter))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=String_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=String_implementations=4.verified.txt new file mode 100644 index 000000000..cdb72f8bb --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=String_implementations=4.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=String_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=String_implementations=6.verified.txt new file mode 100644 index 000000000..c3ee24bf2 --- /dev/null +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=String_implementations=6.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))] + [MongoDB.Bson.Serialization.Attributes.BsonSerializer(typeof(MyTestIdMongoSerializer))] + 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 class DapperTypeHandler : Dapper.SqlMapper.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 MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + { + public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) + { + return new MyTestId(context.Reader.ReadString()); + } + + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) + { + context.Writer.WriteString(value.Value); + } + } + } From 6fdaeab5706cc90e9f26fb73671d2a63eb10a9a3 Mon Sep 17 00:00:00 2001 From: Andrew Lock Date: Sun, 15 May 2022 22:40:47 +0100 Subject: [PATCH 10/12] Remove extra spaces --- src/StronglyTypedIds/Templates/ObjectId/ObjectId_Base.cs | 1 - ...rter=0_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...rter=0_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...rter=0_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...rter=0_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...er=100_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...er=100_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...er=100_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...er=100_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...er=102_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...er=102_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...er=102_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...er=102_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...er=104_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...er=104_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...er=104_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...er=104_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...er=106_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...er=106_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...er=106_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...er=106_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...er=108_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...er=108_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...er=108_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...er=108_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=10_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=10_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=10_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=10_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...er=110_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...er=110_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...er=110_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...er=110_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...er=112_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...er=112_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...er=112_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...er=112_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...er=114_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...er=114_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...er=114_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...er=114_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...er=116_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...er=116_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...er=116_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...er=116_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...er=118_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...er=118_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...er=118_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...er=118_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...er=120_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...er=120_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...er=120_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...er=120_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...er=122_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...er=122_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...er=122_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...er=122_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...er=124_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...er=124_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...er=124_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...er=124_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...er=126_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...er=126_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...er=126_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...er=126_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=12_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=12_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=12_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=12_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=14_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=14_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=14_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=14_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=16_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=16_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=16_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=16_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=18_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=18_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=18_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=18_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=20_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=20_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=20_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=20_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=22_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=22_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=22_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=22_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=24_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=24_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=24_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=24_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=26_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=26_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=26_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=26_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=28_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=28_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=28_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=28_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...rter=2_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...rter=2_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...rter=2_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...rter=2_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=30_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=30_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=30_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=30_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=32_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=32_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=32_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=32_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=34_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=34_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=34_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=34_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=36_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=36_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=36_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=36_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=38_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=38_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=38_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=38_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=40_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=40_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=40_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=40_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=42_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=42_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=42_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=42_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=44_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=44_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=44_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=44_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=46_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=46_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=46_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=46_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=48_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=48_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=48_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=48_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...rter=4_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...rter=4_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...rter=4_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...rter=4_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=50_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=50_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=50_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=50_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=52_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=52_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=52_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=52_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=54_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=54_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=54_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=54_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=56_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=56_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=56_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=56_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=58_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=58_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=58_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=58_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=60_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=60_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=60_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=60_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=62_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=62_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=62_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=62_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=64_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=64_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=64_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=64_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=66_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=66_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=66_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=66_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=68_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=68_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=68_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=68_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...rter=6_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...rter=6_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...rter=6_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...rter=6_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=70_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=70_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=70_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=70_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=72_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=72_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=72_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=72_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=74_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=74_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=74_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=74_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=76_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=76_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=76_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=76_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=78_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=78_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=78_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=78_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=80_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=80_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=80_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=80_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=82_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=82_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=82_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=82_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=84_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=84_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=84_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=84_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=86_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=86_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=86_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=86_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=88_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=88_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=88_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=88_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...rter=8_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...rter=8_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...rter=8_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...rter=8_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=90_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=90_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=90_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=90_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=92_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=92_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=92_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=92_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=94_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=94_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=94_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=94_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=96_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=96_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=96_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=96_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=98_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=98_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=98_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=98_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...rter=0_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...rter=0_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...rter=0_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...rter=0_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...er=100_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...er=100_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...er=100_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...er=100_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...er=102_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...er=102_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...er=102_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...er=102_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...er=104_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...er=104_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...er=104_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...er=104_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...er=106_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...er=106_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...er=106_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...er=106_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...er=108_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...er=108_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...er=108_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...er=108_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=10_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=10_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=10_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=10_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...er=110_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...er=110_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...er=110_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...er=110_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...er=112_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...er=112_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...er=112_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...er=112_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...er=114_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...er=114_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...er=114_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...er=114_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...er=116_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...er=116_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...er=116_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...er=116_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...er=118_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...er=118_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...er=118_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...er=118_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...er=120_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...er=120_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...er=120_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...er=120_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...er=122_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...er=122_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...er=122_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...er=122_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...er=124_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...er=124_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...er=124_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...er=124_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...er=126_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...er=126_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...er=126_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...er=126_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=12_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=12_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=12_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=12_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=14_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=14_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=14_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=14_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=16_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=16_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=16_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=16_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=18_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=18_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=18_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=18_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=20_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=20_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=20_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=20_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=22_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=22_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=22_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=22_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=24_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=24_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=24_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=24_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=26_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=26_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=26_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=26_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=28_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=28_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=28_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=28_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...rter=2_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...rter=2_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...rter=2_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...rter=2_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=30_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=30_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=30_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=30_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=32_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=32_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=32_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=32_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=34_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=34_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=34_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=34_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=36_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=36_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=36_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=36_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=38_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=38_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=38_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=38_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=40_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=40_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=40_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=40_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=42_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=42_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=42_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=42_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=44_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=44_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=44_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=44_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=46_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=46_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=46_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=46_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=48_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=48_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=48_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=48_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...rter=4_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...rter=4_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...rter=4_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...rter=4_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=50_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=50_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=50_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=50_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=52_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=52_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=52_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=52_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=54_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=54_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=54_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=54_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=56_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=56_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=56_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=56_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=58_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=58_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=58_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=58_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=60_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=60_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=60_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=60_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=62_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=62_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=62_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=62_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=64_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=64_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=64_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=64_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=66_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=66_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=66_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=66_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=68_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=68_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=68_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=68_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...rter=6_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...rter=6_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...rter=6_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...rter=6_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=70_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=70_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=70_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=70_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=72_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=72_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=72_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=72_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=74_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=74_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=74_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=74_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=76_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=76_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=76_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=76_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=78_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=78_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=78_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=78_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=80_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=80_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=80_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=80_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=82_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=82_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=82_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=82_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=84_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=84_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=84_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=84_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=86_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=86_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=86_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=86_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=88_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=88_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=88_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=88_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...rter=8_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...rter=8_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...rter=8_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...rter=8_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=90_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=90_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=90_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=90_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=92_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=92_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=92_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=92_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=94_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=94_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=94_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=94_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=96_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=96_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=96_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=96_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- ...ter=98_backingType=ObjectId_implementations=0.verified.txt | 4 ++-- ...ter=98_backingType=ObjectId_implementations=2.verified.txt | 4 ++-- ...ter=98_backingType=ObjectId_implementations=4.verified.txt | 4 ++-- ...ter=98_backingType=ObjectId_implementations=6.verified.txt | 4 ++-- 513 files changed, 1024 insertions(+), 1025 deletions(-) diff --git a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_Base.cs b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_Base.cs index fc664a6e4..93f32aad7 100644 --- a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_Base.cs +++ b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_Base.cs @@ -22,4 +22,3 @@ public override bool Equals(object obj) public override string ToString() => Value.ToString(); public static bool operator ==(TESTID a, TESTID b) => a.Equals(b); public static bool operator !=(TESTID a, TESTID b) => !(a == b); - \ No newline at end of file diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=0.verified.txt index 1b266fc0d..5716eb6fc 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,5 +35,5 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - } + } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=2.verified.txt index f45358787..497986bb0 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,5 +35,5 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - } + } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=4.verified.txt index add723dbc..a2c88d270 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,6 +35,6 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=6.verified.txt index 584b84a69..9d0bc5c9b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=0_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,6 +35,6 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=0.verified.txt index 1cbdf83e0..797ff736d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=2.verified.txt index c255b2036..aa2a25aa9 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=4.verified.txt index 8c52540e4..f0910cce6 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=6.verified.txt index f243643cb..9f98406e2 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=0.verified.txt index 0ad4e0dea..902b2f2ca 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=2.verified.txt index 8cf3f12d4..cbdd8fd5e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=4.verified.txt index b1d594d43..a7195a763 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=6.verified.txt index e9dfc89ff..b6043b145 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=0.verified.txt index f22ee19c9..eeb63ae90 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=2.verified.txt index 3bb408b60..07a701cb7 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=4.verified.txt index 0eaa1cd61..e9364bf9b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=6.verified.txt index bca347f7f..b64aa71a7 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=0.verified.txt index 16172c01d..1dde94e61 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=2.verified.txt index 4f76bc2fe..65c9c269a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=4.verified.txt index 139a0957a..2a51b1fe1 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=6.verified.txt index 906dc5d92..acae48b83 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=0.verified.txt index bc367c45f..80d07093c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=2.verified.txt index 40bf7b2e1..7e998890d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=4.verified.txt index 08f7b7031..52eb0b2cd 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=6.verified.txt index da67f92da..4c45daf7a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=0.verified.txt index 19924d32e..1dc0ce08c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=2.verified.txt index f8c49ed65..0b31b44ef 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=4.verified.txt index 6e1f7d3f4..4bdba08cf 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=6.verified.txt index 5691cd811..fe291f15d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=0.verified.txt index 0662a1f1e..2e8a6953f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -39,7 +39,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=2.verified.txt index dd782afce..277bd7750 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -39,7 +39,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=4.verified.txt index 632b4d0c5..e3402db2f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -39,7 +39,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=6.verified.txt index df3915846..98ea39718 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -39,7 +39,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=0.verified.txt index b6a730915..7621033b4 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=2.verified.txt index ca03bd036..8e32835a7 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=4.verified.txt index 3b43108cf..3ea7a5658 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=6.verified.txt index 91c4934f9..bcfcaad28 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=0.verified.txt index 35afc1bfc..a9406cebe 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=2.verified.txt index 2e1b67d27..d3d33cc49 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=4.verified.txt index 851f0d500..51ac2e0b6 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=6.verified.txt index cf12b103c..42881f13e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=0.verified.txt index a735dab0e..89d44b942 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=2.verified.txt index 92a942a6a..e0c124bda 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=4.verified.txt index 9dbb4dd5e..b9e0eec34 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=6.verified.txt index 1c37a7f68..4634f0428 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=0.verified.txt index ab528e9cf..fd7a45926 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=2.verified.txt index af2211968..d5c032cdc 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=4.verified.txt index e98d4a936..eec74ab0e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=6.verified.txt index 86ed3587f..6c705f3ab 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=0.verified.txt index 907c977b8..bec774c64 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=2.verified.txt index 422f6462f..4e33b967b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=4.verified.txt index 5382e0fd3..6139410f4 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=6.verified.txt index b8af3dc37..a203813f5 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=0.verified.txt index b974f86f4..0a485c6b6 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=2.verified.txt index 015eced30..73f8b52cc 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=4.verified.txt index 68b79cdc4..b02568605 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=6.verified.txt index d2ee9cab5..173926f1f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=0.verified.txt index 5e3d54e96..f17a9d0fa 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=2.verified.txt index d00b1604a..d82a2535b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=4.verified.txt index ae70492e9..3f9df0183 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=6.verified.txt index 84d567a0a..e9087e4a0 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=0.verified.txt index f33a94029..9afaf334e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -39,7 +39,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=2.verified.txt index 561fffcaa..01e9f6f97 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -39,7 +39,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=4.verified.txt index eb0454015..dba3fe231 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -39,7 +39,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=6.verified.txt index 7828a25c0..bb0706b5d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -39,7 +39,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=0.verified.txt index 1607fac4b..1a88668ca 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter { public override bool CanConvert(System.Type objectType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=2.verified.txt index c337ee109..06b4af719 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter { public override bool CanConvert(System.Type objectType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=4.verified.txt index 286e53c17..65e8549ec 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=6.verified.txt index 682ebff32..b26443411 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=0.verified.txt index ab13cd74a..967685186 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=2.verified.txt index 7ca904598..5dd4b4798 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=4.verified.txt index 78f552342..e965baeb8 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=6.verified.txt index 4afc73b7d..8f762955c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=0.verified.txt index 936809ad4..50d76bc6c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=2.verified.txt index 2590cfd59..28b3bbb52 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=4.verified.txt index bc835b8b1..21597ff58 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=6.verified.txt index aa122e9a5..679226165 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=0.verified.txt index 16de8d48a..772e2d55f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=2.verified.txt index 01ce0e706..ef0c4872a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=4.verified.txt index d2d51504e..211175d3e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=6.verified.txt index cc4f11f52..ef5b4fedb 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=0.verified.txt index 2aec1a34e..520d8923f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=2.verified.txt index 111b2fec8..b168efacc 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=4.verified.txt index e1a7754de..dd028c38e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=6.verified.txt index 21f5d46bd..c4e673b86 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=0.verified.txt index f14d08fb5..b8442c07b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=2.verified.txt index d4ec71de8..1b98e6062 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=4.verified.txt index ca3116b05..584230a5f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=6.verified.txt index fbe393574..4a825bcbe 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=0.verified.txt index 354c66d90..4a95ad4f5 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=2.verified.txt index 9b4db18e0..b874d2da7 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=4.verified.txt index 872efd5b2..7507935a4 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=6.verified.txt index 11106de1a..3e681f474 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=0.verified.txt index f7d6b51e1..3e8d29e56 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=2.verified.txt index b5bcbf504..2f0fa6dbc 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=4.verified.txt index d42eb628a..94ffca479 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=6.verified.txt index f9043b224..191b3d14a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=0.verified.txt index d50e970f9..867a06cc3 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=2.verified.txt index 9c531fd53..1510f75c3 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=4.verified.txt index 9b113fe0e..f46a29ae4 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=6.verified.txt index a2de8f716..13645b411 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=ObjectId_implementations=0.verified.txt index 04bd5ea00..916d6807f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=ObjectId_implementations=2.verified.txt index f53b81178..5605af838 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=ObjectId_implementations=4.verified.txt index 5e670cb7d..884417f74 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=ObjectId_implementations=6.verified.txt index 781e0bc04..78083b325 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=2_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=0.verified.txt index 9781a02ef..c6d7dae41 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=2.verified.txt index e08bd1786..e37a85763 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=4.verified.txt index 8dbfd6a20..6a0510db5 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=6.verified.txt index b398b194e..d97888a2e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=0.verified.txt index 6b96687f7..6c0290055 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=2.verified.txt index 6eecbeefd..42e869041 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=4.verified.txt index bea4f7dee..8fd6f46f5 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=6.verified.txt index 7a35073e6..a3a579249 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=0.verified.txt index cfbcba487..111145a83 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=2.verified.txt index 056dc9d44..030cd4b60 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=4.verified.txt index a39aa32ee..565e1f968 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=6.verified.txt index ad437f37c..9278e4ec5 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=0.verified.txt index 1d12e6e28..0748b42af 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=2.verified.txt index 29965b640..221396511 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=4.verified.txt index 67fd16818..ecb9cb32c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=6.verified.txt index e7b77b62b..43ca11547 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=0.verified.txt index 8ec8de183..6986de043 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=2.verified.txt index 28608d940..dc5c374c0 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=4.verified.txt index ee045605b..f772bcb82 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=6.verified.txt index 0206ba250..3185cced3 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=0.verified.txt index b3ee5fabb..c7a0a766a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=2.verified.txt index 5476f0598..2bdc53793 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=4.verified.txt index 1779a87be..7a4791d4d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=6.verified.txt index ce337bc91..8e8a2d151 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=0.verified.txt index 0042da41d..7110c77cc 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=2.verified.txt index f8c96a97b..33183634b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=4.verified.txt index 379eeadce..189c0ec80 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=6.verified.txt index 54b708901..608cbfd4f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=0.verified.txt index 38aaf3223..73e1deac8 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=2.verified.txt index 069181983..40c26da6b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=4.verified.txt index 16ec89b08..d7af3eb91 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=6.verified.txt index 8becbb788..5516d8532 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=0.verified.txt index da3783ce4..29b33ebd9 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=2.verified.txt index 4cf7e8b55..fb265f663 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=4.verified.txt index 0dffcd215..2793abfa1 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=6.verified.txt index fc671981e..b866651fe 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=0.verified.txt index 18132f41a..b9555daee 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=2.verified.txt index 36305bc9d..50551a3ed 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=4.verified.txt index d17c85845..aff239aa6 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=6.verified.txt index e7dd7e217..0b2704b3b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=0.verified.txt index a902afbad..78d12cba5 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter { public override bool CanConvert(System.Type objectType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=2.verified.txt index d572266bd..fea43dc3e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter { public override bool CanConvert(System.Type objectType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=4.verified.txt index a9bf0834f..019baffb7 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=6.verified.txt index 7e1bfb232..4b942da94 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=0.verified.txt index 64f1b80ad..1fce9057e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=2.verified.txt index b4ae0635b..4c81fbd6c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=4.verified.txt index d0165ba3c..87c9003ec 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=6.verified.txt index cbc1bd4b0..6c7f07352 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=0.verified.txt index 71ac4d604..c1a4c30ad 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=2.verified.txt index 873335891..6a00062c4 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=4.verified.txt index 6b6fe1fc8..ea363553f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=6.verified.txt index 7c442de5f..16e2811f8 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=0.verified.txt index 7d1892ec4..6592b6e57 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=2.verified.txt index 34edc36ac..d6488d348 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=4.verified.txt index bfdddf99a..565d65f0a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=6.verified.txt index c212c2bf6..49993ef3c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=0.verified.txt index 811969e3b..87a1a6693 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=2.verified.txt index 676e302ab..3821e15a8 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=4.verified.txt index f94fe7e39..c3ddb6a29 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=6.verified.txt index 43fd9fd24..f683b7d90 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=0.verified.txt index 4ddea4d7e..868c5d4a8 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=2.verified.txt index 13ff81e51..50b0e86e7 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=4.verified.txt index caead2ffd..bc8d516c9 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=6.verified.txt index 4d0552ffb..4213cf9a9 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=0.verified.txt index a7c8dc8c8..4efdc419f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=2.verified.txt index 0936bb50e..27478fb26 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=4.verified.txt index 0250ffd9d..5d5ef9beb 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=6.verified.txt index 141948ad2..3fe2f67fb 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=0.verified.txt index e2d02e0d2..eb25cea56 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=2.verified.txt index 09687e781..344e33e6d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=4.verified.txt index a4a770fa0..431c141fd 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=6.verified.txt index 9e396c185..c52f44d88 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=0.verified.txt index 5d34206ae..b68500092 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=2.verified.txt index a536c87af..6186e8036 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=4.verified.txt index 3918d4579..ed092191d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=6.verified.txt index fd1f4aa98..06438ed72 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=0.verified.txt index 69ae48dc0..822719cda 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=2.verified.txt index 154f00b84..3caf1e945 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=4.verified.txt index fc3f5e975..f32050824 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=6.verified.txt index 40fc4cf07..4eed8dd42 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=0.verified.txt index 55121a51d..be088fc82 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter { public override bool CanConvert(System.Type objectType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=2.verified.txt index 5f1844f6a..2ff18fa52 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter { public override bool CanConvert(System.Type objectType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=4.verified.txt index 806520188..718b671f6 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=6.verified.txt index 42003d870..700063d59 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=0.verified.txt index da34c0ef0..4292638de 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=2.verified.txt index 4d2a3bb46..f0a5dd3a0 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=4.verified.txt index 7e8e5f205..eba757f81 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=6.verified.txt index c29ff7770..bd6c3bda0 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=0.verified.txt index 95f0889d5..95c29e60b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=2.verified.txt index f9f63919e..527f09ecb 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=4.verified.txt index c629fe564..54533dc7e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=6.verified.txt index 12919ab03..9e2825375 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=0.verified.txt index a47042d18..868529691 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=2.verified.txt index 68040e96e..5cb21c7bb 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=4.verified.txt index 943eb6343..36ac776a8 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=6.verified.txt index f2a600f09..343837c0a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=0.verified.txt index 57f35fc0e..59bb78753 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=2.verified.txt index 379b4cf90..0687532df 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=4.verified.txt index ab64cf45a..df5bc965b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=6.verified.txt index dabdafbad..6e408ce6c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=0.verified.txt index 579f44b66..f98d90086 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter { public override bool CanConvert(System.Type objectType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=2.verified.txt index 210b5e58d..2d48a13ad 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter { public override bool CanConvert(System.Type objectType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=4.verified.txt index 270590345..0b9f6d98e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=6.verified.txt index e6c0afd37..ff4850832 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=0.verified.txt index 6d5b97c8f..b24c74f66 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -39,7 +39,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=2.verified.txt index 25ab8031d..c9796d743 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -39,7 +39,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=4.verified.txt index d144203fb..a48952a71 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -39,7 +39,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=6.verified.txt index 6f321ddaf..ddae4e62e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -39,7 +39,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=0.verified.txt index 28eb2e284..cd2558e33 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=2.verified.txt index c26cef75f..789be5adc 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=4.verified.txt index 544f0ddc0..bebdd750f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=6.verified.txt index 4b8b86738..23f29e31c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=0.verified.txt index 757314d67..8984bf4c8 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=2.verified.txt index b0013e194..91390a303 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=4.verified.txt index 5e15f210f..bce5627ef 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=6.verified.txt index 7595847c1..b4f31f0a6 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=0.verified.txt index b594f689e..e600a9375 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=2.verified.txt index bf58a0061..452824c83 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=4.verified.txt index 94f03e0f3..fffa6613c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=6.verified.txt index 5809c8a99..5cdb7a0e0 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=0.verified.txt index 4b1f91f59..68d04752b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=2.verified.txt index 21798830f..ce52e0639 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=4.verified.txt index cc2429c32..268f2e382 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=6.verified.txt index 2c6f11c52..518ab7459 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=0.verified.txt index 6d7116a79..b21981b64 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=2.verified.txt index 9643086f0..ce2f017e0 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=4.verified.txt index b662eed99..733cc8406 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=6.verified.txt index 786fa7e40..42082210a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=0.verified.txt index 96551e3b2..e61f5ff6e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=2.verified.txt index dc5ccc772..0bcd7f262 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=4.verified.txt index 601afddc6..5f4495750 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=6.verified.txt index 1846e3647..12833a700 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=0.verified.txt index 79961e06d..5916ec3f9 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=2.verified.txt index be24df22e..c22c346c0 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=4.verified.txt index bd138b95a..dd89afa68 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=6.verified.txt index c80fb8982..d17dea51e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=0.verified.txt index 6b2f0148f..703372d91 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=2.verified.txt index 018bc4d1d..5205031e7 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=4.verified.txt index c6309dd05..7375671aa 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=6.verified.txt index ff44c4b90..e78d916c5 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,7 +38,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=0.verified.txt index 21513e9a3..e32c7d34b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -39,7 +39,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=2.verified.txt index e6f27daac..8b4cf13bf 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -39,7 +39,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=4.verified.txt index 452cd398d..730411029 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -39,7 +39,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=6.verified.txt index a79f8ef7c..b8d247392 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -39,7 +39,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=0.verified.txt index fe5a05b0c..8dbae2bd5 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=2.verified.txt index d4963b9d9..181a1843e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=4.verified.txt index 009dcb47a..75e119c92 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=6.verified.txt index 24f623155..e6a12720d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=0.verified.txt index acf664d88..593494065 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=2.verified.txt index 467621541..2a1c38a75 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=4.verified.txt index d8103c622..a88950c55 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=6.verified.txt index feb59da8e..9a2ffd87e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ namespace Some.Namespace public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=ObjectId_implementations=0.verified.txt index 921686a2d..361f73958 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -33,4 +33,4 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=ObjectId_implementations=2.verified.txt index 3217d3090..fa93060cc 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -33,4 +33,4 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - } + } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=ObjectId_implementations=4.verified.txt index b51b79f13..63c78bb31 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -33,5 +33,5 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=ObjectId_implementations=6.verified.txt index b7c6a61cc..240fe27da 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=0_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -33,5 +33,5 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=0.verified.txt index bacfe62ad..2aa8286d2 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=2.verified.txt index 9baca3eff..12cdb21a3 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=4.verified.txt index 1eb98819d..78a6be997 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=6.verified.txt index c9545a267..3a7798cec 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=0.verified.txt index 9e10cc0a6..18d35bc8c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=2.verified.txt index 272f643ac..1bacbaa7b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=4.verified.txt index c3b9860ee..a41286e0d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=6.verified.txt index c69451203..5c1e0f178 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=0.verified.txt index 15c7e8834..9b40e29bd 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=2.verified.txt index 0fc03749f..9978e686f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=4.verified.txt index b86c8d3c3..60140ae5b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=6.verified.txt index 71fac4eda..b26f787db 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=0.verified.txt index 9a27dd6dc..caa149449 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=2.verified.txt index a7bd1b439..1af9883c3 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=4.verified.txt index 1df765502..2ce1b9aa4 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=6.verified.txt index 6a9906212..f860d0212 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=0.verified.txt index d610b0625..748e15c62 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=2.verified.txt index 0b1ad79f0..62a77014f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=4.verified.txt index 7c0b3a847..fda381e52 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=6.verified.txt index 319264c3a..39712c051 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=0.verified.txt index 843f02da5..2c4eb6795 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=2.verified.txt index a901995e9..3f14fc328 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=4.verified.txt index 05ed6addb..ff7c33cdb 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=6.verified.txt index 88bb91749..fec485d6a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=0.verified.txt index 571f1903e..0a6c42cda 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=2.verified.txt index e04710ed2..dc1054711 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=4.verified.txt index 83312b51a..ec2f3691f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=6.verified.txt index 333647949..f697e0c9b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=0.verified.txt index 41aedf49a..b96036480 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=2.verified.txt index 6d6f396fd..2f1df41fc 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=4.verified.txt index 38b28b8df..2fb8e553d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=6.verified.txt index 96e7d6255..8cd8538e9 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=0.verified.txt index 9d3904768..d38a16950 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=2.verified.txt index f675ba5d1..21807a7b9 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=4.verified.txt index 90e3475b7..aaae129b7 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=6.verified.txt index 1d88fd487..a9f31ed30 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=0.verified.txt index 3144a199c..eb7892945 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=2.verified.txt index bb50b2dfd..9f1508f9b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=4.verified.txt index 6ce7f9019..2c98c1c09 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=6.verified.txt index f94f316f0..d1fa8ba18 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=0.verified.txt index 045dd5aab..b4f2ef7a6 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=2.verified.txt index 8f6299206..8c77b674e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=4.verified.txt index 7d909309a..91428baec 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=6.verified.txt index 445ce0667..e7ebea21f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=0.verified.txt index abd8feb10..989320ad3 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=2.verified.txt index 94b712b1e..4d435c913 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=4.verified.txt index 2ccd1532d..b65ad4e64 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=6.verified.txt index 32ae9eb7b..96011e369 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=0.verified.txt index 34edcfbc0..8a23d59a7 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=2.verified.txt index f5707e28d..0e1ffb029 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=4.verified.txt index 7d772411a..4aeedf808 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=6.verified.txt index a20e4d1d9..974b64827 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=0.verified.txt index 931efc347..6946e230f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=2.verified.txt index 44dd298a4..7e6702d29 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=4.verified.txt index a0ac681d7..8f67ce49d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=6.verified.txt index 2c1e4b81e..9bda1971e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=0.verified.txt index 1b69ebc7d..c57f2819c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=2.verified.txt index 1d1c030d3..239c86080 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=4.verified.txt index 7fdc59ae9..db543a68b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=6.verified.txt index 37a385fac..e3d192507 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=0.verified.txt index 2621e45e2..8826bd65b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter { public override bool CanConvert(System.Type objectType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=2.verified.txt index 13d5e3318..3c26bcdd5 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter { public override bool CanConvert(System.Type objectType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=4.verified.txt index 65f6776e0..1899013b0 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=6.verified.txt index c090468cb..bb91b65bc 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=0.verified.txt index 0d4c9b11e..e4687a2d7 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=2.verified.txt index 82bc98e5b..2acfc5504 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=4.verified.txt index 0aadeaa16..84795d760 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=6.verified.txt index 178119bc0..520bb8c86 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=0.verified.txt index ba23bc1a2..946abf89f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -33,7 +33,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=2.verified.txt index 51523c90b..7296ba27c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -33,7 +33,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=4.verified.txt index 5b8b8b970..b47d9a55c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -33,7 +33,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=6.verified.txt index 2302d348d..e4375d918 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -33,7 +33,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=0.verified.txt index 6e7c69adb..2d4db48cf 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=2.verified.txt index c277d28d6..bf40c7933 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=4.verified.txt index eabf7fd63..d09f1a413 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=6.verified.txt index fc71d3a78..1c3450550 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=0.verified.txt index 72a6b7846..c973dd8b1 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=2.verified.txt index 5c2453cf7..1c1eb578a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=4.verified.txt index 010dd1362..502519c68 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=6.verified.txt index 000f16d85..f3b7583b1 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=0.verified.txt index e6ca63341..9baf34997 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=2.verified.txt index beed85030..2cfe04037 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=4.verified.txt index 57a5e7aa4..06be3997c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=6.verified.txt index 28526dddd..af5ca6872 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=0.verified.txt index df4d31b6d..4833bda84 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=2.verified.txt index 3ef3a8e63..0265e6eb2 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=4.verified.txt index f2dee4335..dddf7bdd8 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=6.verified.txt index d1e2c5ee3..2c469d5fe 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=0.verified.txt index 659a37838..e0decd7a2 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=2.verified.txt index ffe832d5f..3db33131d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=4.verified.txt index 00bb4b549..bcb226936 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=6.verified.txt index 2f648a037..0d7336feb 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=0.verified.txt index ec2515165..500622f19 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=2.verified.txt index 22ad494ee..0bebc9f0c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=4.verified.txt index b2b28fe2b..80591cbad 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=6.verified.txt index e028b8fc0..7bf8266ca 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=ObjectId_implementations=0.verified.txt index 87adcaf74..a059171a3 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=ObjectId_implementations=2.verified.txt index aadbddf17..963d85998 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=ObjectId_implementations=4.verified.txt index 2ac70d923..7fe942bb5 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=ObjectId_implementations=6.verified.txt index 7ca8f621a..52456cfd1 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=2_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=0.verified.txt index 75e46cb5e..22578483c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=2.verified.txt index 9ad852aab..613ab38f4 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=4.verified.txt index f941793ff..3b1ce8b66 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=6.verified.txt index 4a168987f..100b380ac 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=0.verified.txt index 84eb1273a..731a4103f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -33,7 +33,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=2.verified.txt index f7fc1267b..0992de8e2 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -33,7 +33,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=4.verified.txt index dbf784590..fb3536d4c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -33,7 +33,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=6.verified.txt index e09cff614..9957dc45a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -33,7 +33,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=0.verified.txt index aad89de0e..492dd3661 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=2.verified.txt index 4fac08c3c..31bc4d663 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=4.verified.txt index 49a78c4de..e0e07e5fd 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=6.verified.txt index 7c6e818d6..f77924cbe 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=0.verified.txt index 143409604..fdf28f480 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=2.verified.txt index 5c2e808f9..92671c34e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=4.verified.txt index 6645dd7d3..eca9e8c45 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=6.verified.txt index 5df3fc3bd..ce53460fc 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=0.verified.txt index 85f156a5c..b25657d46 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=2.verified.txt index f605f8b30..b17bd616b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=4.verified.txt index 4adc27f81..9ec4c7267 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=6.verified.txt index 781f833aa..deb6cbd05 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=0.verified.txt index 0ce301beb..e8ac4ce46 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=2.verified.txt index ea209d846..74c91594e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=4.verified.txt index b41b6c0a6..9f51081a2 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=6.verified.txt index 0b651168c..8e168c150 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=0.verified.txt index 8d6384329..c74c87448 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=2.verified.txt index 9d6764288..9bf13edd2 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=4.verified.txt index db6748745..9f34eb88f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=6.verified.txt index bdfc20ca6..b6cae9fae 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=0.verified.txt index 986322f52..99fca607b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=2.verified.txt index 3e9a14f55..52385d538 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=4.verified.txt index 026157252..3ebdab09c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=6.verified.txt index ba0502002..f63aea6f8 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=0.verified.txt index e23b13455..706d1c5e3 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=2.verified.txt index 01fd13f33..eb21ddc48 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=4.verified.txt index e6b71d400..a6c18fa0d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=6.verified.txt index 79856c3b4..8ab0011b6 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=0.verified.txt index 3720d6d78..3a8b6f2ee 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -33,7 +33,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=2.verified.txt index c64aa3394..b85c96ad6 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -33,7 +33,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=4.verified.txt index c9fcf1fb5..cd5e4c7cb 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -33,7 +33,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=6.verified.txt index 09451a98e..2ef1423c6 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -33,7 +33,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=0.verified.txt index 4ab2d843c..645ad6d26 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter { public override bool CanConvert(System.Type objectType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=2.verified.txt index 0043961dd..d2f67bb47 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter { public override bool CanConvert(System.Type objectType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=4.verified.txt index 76b8ef930..b62144637 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=6.verified.txt index 6b7513235..22cffa42f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=0.verified.txt index 2218d209e..3b567084f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=2.verified.txt index c881adbc9..8d0e418ea 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=4.verified.txt index 725b8abf6..a4fd418d0 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=6.verified.txt index 042104e32..c9a961c6e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=0.verified.txt index 1dce32451..2552d9ee4 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=2.verified.txt index a2b0658cb..3dd42d233 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=4.verified.txt index 594365f90..23667c56c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=6.verified.txt index ac67d3102..b4e019091 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=0.verified.txt index 8c9bf07a4..74c4b42e2 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=2.verified.txt index a28cd1170..fa6f711fc 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=4.verified.txt index effe0fa4b..5f94a690b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=6.verified.txt index d20e75c76..99a2e8e0b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=0.verified.txt index 9de1d9892..7c1825848 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=2.verified.txt index d2d4214fc..7c37d9165 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=4.verified.txt index 7a286ca44..5f9467982 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=6.verified.txt index aab7999ad..7e8dff87e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=0.verified.txt index 8271ac1f5..4d2a69419 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=2.verified.txt index 9418d2304..2e0ba5a1a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=4.verified.txt index 4ec635df4..4ca6256a7 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=6.verified.txt index c4e2be596..c0bf78cbb 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=0.verified.txt index d67f18b4a..7c35bd80a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=2.verified.txt index 61b0ec9d5..d9a70faa1 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=4.verified.txt index b2e94c03e..0d0d676e0 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=6.verified.txt index 3048f67ab..f8b64eaf3 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=0.verified.txt index bcaeec478..8fd9d3fa1 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=2.verified.txt index 4dff83c9f..fca5e697d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=4.verified.txt index 14aabde7f..010eaf01a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=6.verified.txt index 74f46cdb1..8ff488730 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=0.verified.txt index 565f692ae..95dc624f8 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=2.verified.txt index b2744c085..770b65772 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase + class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=4.verified.txt index a393bbe8a..43b535812 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=6.verified.txt index da0d8c3af..d217ef7c0 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=0.verified.txt index 23539c3d9..798742c49 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=2.verified.txt index eaf6e07b3..4aba2cf44 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=4.verified.txt index 2c01c313a..99b6d721e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=6.verified.txt index 57d200982..e915acb10 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=0.verified.txt index 73837ad61..0b1edfa61 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter { public override bool CanConvert(System.Type objectType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=2.verified.txt index 063f33a17..710a68733 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter { public override bool CanConvert(System.Type objectType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=4.verified.txt index 3491b714b..173438c75 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=6.verified.txt index 685735df9..e4a3cbaa5 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=0.verified.txt index 9954a512d..5887eedce 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=2.verified.txt index e28e9a710..8588bf8f2 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=4.verified.txt index a763f7a43..5d8b70573 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=6.verified.txt index aecd6c4ba..c4c1b13e2 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=0.verified.txt index 25c2a5266..4344cf503 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=2.verified.txt index 420220cb9..e679429d5 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=4.verified.txt index 43058e788..d66d1f68a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=6.verified.txt index c0ffb59d1..37b6738db 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=0.verified.txt index e7a289d90..c2cfe7b4d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=2.verified.txt index bb1090982..9ddf04f87 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=4.verified.txt index 465ed5d1f..30fc1b08b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=6.verified.txt index 0a7cdb2d0..333f1314c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=0.verified.txt index 7291d9ed7..f142ab759 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=2.verified.txt index 19501dc98..add6eb09c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=4.verified.txt index 75fa39373..3a33a59d8 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=6.verified.txt index a857313cd..be3b10103 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=0.verified.txt index 16f297244..66ddeea3c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter { public override bool CanConvert(System.Type objectType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=2.verified.txt index 6d398eba5..8c286d71e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter { public override bool CanConvert(System.Type objectType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=4.verified.txt index 254d2b0ed..83b22c256 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=6.verified.txt index 15f5ae03f..f63a43679 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdNewtonsoftJsonConverter : Newtonsoft.Json.JsonConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=0.verified.txt index b5ed196f5..0b5ab0638 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=2.verified.txt index 0de2b1993..63cbe303e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { public override bool CanConvertFrom(System.ComponentModel.ITypeDescriptorContext context, System.Type sourceType) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=4.verified.txt index b8a6a0b07..cf4d42f1d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=6.verified.txt index 1cd6f80db..86aaa2ec8 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdTypeConverter : System.ComponentModel.TypeConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=0.verified.txt index 7a294ed54..88fb2497a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=2.verified.txt index bdff64e42..4adc3c8b7 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=4.verified.txt index 794ce6e6f..f7ea0bd17 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=6.verified.txt index 170a7a62b..434688237 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=0.verified.txt index b86265af8..b80ede2bb 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=2.verified.txt index bc22d3d58..30b59df32 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=4.verified.txt index 0b7a5c3af..bde10beda 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=6.verified.txt index 528d4c265..e16ace8c1 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=0.verified.txt index 5eb341e75..0e42ea001 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=2.verified.txt index b5b818501..75677498e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=4.verified.txt index 32791c7ee..e2a2ce096 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=6.verified.txt index 83ac52244..741cdff18 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=0.verified.txt index e770f5aff..7f59d4411 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=2.verified.txt index 914cac096..b42acdddb 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=4.verified.txt index d9abbb995..9968367aa 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=6.verified.txt index bbc5e2aac..66e02b45b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=0.verified.txt index 6b26a9f45..f41489652 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=2.verified.txt index 0f0c8cb1f..25aff466c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=4.verified.txt index 4568b2590..9965dd0fb 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=6.verified.txt index 10e90de86..db503536a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=0.verified.txt index 47d5235a4..f68a20a1f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=2.verified.txt index 64b586bf9..622878424 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=4.verified.txt index 6bf5c77b4..b3f510157 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=6.verified.txt index 974675111..aa9dd6beb 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); class MyTestIdSystemTextJsonConverter : System.Text.Json.Serialization.JsonConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=0.verified.txt index 1435fe725..fcf753eba 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=2.verified.txt index 5561b8133..393612267 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=4.verified.txt index 616f59560..f8cd31588 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=6.verified.txt index 56669267d..8b337ccbd 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=0.verified.txt index cbc2fd4b8..84455b75e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=2.verified.txt index 5681c0435..8a897a132 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=4.verified.txt index ffdb06b89..28da71911 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=6.verified.txt index 3156f0a48..01eae9492 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -36,7 +36,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=0.verified.txt index 2c0cfbb5c..7efc48c3f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=2.verified.txt index e9fee261f..fdb3f43eb 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { public EfCoreValueConverter() : this(null) { } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=4.verified.txt index 7f9a9bc33..7b818ce29 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=6.verified.txt index 741b21505..a0af79484 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -37,7 +37,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueConversion.ValueConverter { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=0.verified.txt index 84e887c3a..2974365f8 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=2.verified.txt index 407caf1ab..67f2df0e4 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=4.verified.txt index daaad3541..1b872b775 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=6.verified.txt index ac9276e58..27291483f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -34,7 +34,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=0.verified.txt index e912d9f70..bad300b03 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=2.verified.txt index 3965e1f81..03f0e33c8 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - + public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { public override void SetValue(System.Data.IDbDataParameter parameter, MyTestId value) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=4.verified.txt index 78d5894be..d235c2ad8 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=6.verified.txt index 4bfd7e3be..6fd6a8086 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -35,7 +35,7 @@ public override string ToString() => Value.ToString(); public static bool operator ==(MyTestId a, MyTestId b) => a.Equals(b); public static bool operator !=(MyTestId a, MyTestId b) => !(a == b); - public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); + public int CompareTo(MyTestId other) => Value.CompareTo(other.Value); public class DapperTypeHandler : Dapper.SqlMapper.TypeHandler { From a6d0b1b6f29d3e4773da562d4e4306807a6da711 Mon Sep 17 00:00:00 2001 From: Marcin Rogoz Date: Mon, 16 May 2022 17:38:09 +0200 Subject: [PATCH 11/12] CodeReview suggestion about returing TESTID.Empty --- .../ObjectId/ObjectId_DapperTypeHandler.cs | 1 - .../ObjectId/ObjectId_EfCoreValueConverter.cs | 4 +- .../ObjectId/ObjectId_MongoSerializer.cs | 21 ++------- .../ObjectId_NewtonsoftJsonConverter.cs | 12 ++---- .../ObjectId_SystemTextJsonConverter.cs | 3 +- .../ObjectIdTests.cs | 23 ++-------- ...pe=ObjectId_implementations=0.verified.txt | 36 +++------------- ...pe=ObjectId_implementations=2.verified.txt | 36 +++------------- ...pe=ObjectId_implementations=4.verified.txt | 36 +++------------- ...pe=ObjectId_implementations=6.verified.txt | 36 +++------------- ...pe=ObjectId_implementations=0.verified.txt | 36 +++------------- ...pe=ObjectId_implementations=2.verified.txt | 36 +++------------- ...pe=ObjectId_implementations=4.verified.txt | 36 +++------------- ...pe=ObjectId_implementations=6.verified.txt | 36 +++------------- ...pe=ObjectId_implementations=0.verified.txt | 27 +++--------- ...pe=ObjectId_implementations=2.verified.txt | 27 +++--------- ...pe=ObjectId_implementations=4.verified.txt | 27 +++--------- ...pe=ObjectId_implementations=6.verified.txt | 27 +++--------- ...pe=ObjectId_implementations=0.verified.txt | 27 +++--------- ...pe=ObjectId_implementations=2.verified.txt | 27 +++--------- ...pe=ObjectId_implementations=4.verified.txt | 27 +++--------- ...pe=ObjectId_implementations=6.verified.txt | 27 +++--------- ...pe=ObjectId_implementations=0.verified.txt | 39 ++++------------- ...pe=ObjectId_implementations=2.verified.txt | 39 ++++------------- ...pe=ObjectId_implementations=4.verified.txt | 39 ++++------------- ...pe=ObjectId_implementations=6.verified.txt | 39 ++++------------- ...pe=ObjectId_implementations=0.verified.txt | 5 +-- ...pe=ObjectId_implementations=2.verified.txt | 5 +-- ...pe=ObjectId_implementations=4.verified.txt | 5 +-- ...pe=ObjectId_implementations=6.verified.txt | 5 +-- ...pe=ObjectId_implementations=0.verified.txt | 39 ++++------------- ...pe=ObjectId_implementations=2.verified.txt | 39 ++++------------- ...pe=ObjectId_implementations=4.verified.txt | 39 ++++------------- ...pe=ObjectId_implementations=6.verified.txt | 39 ++++------------- ...pe=ObjectId_implementations=0.verified.txt | 28 +++--------- ...pe=ObjectId_implementations=2.verified.txt | 28 +++--------- ...pe=ObjectId_implementations=4.verified.txt | 28 +++--------- ...pe=ObjectId_implementations=6.verified.txt | 28 +++--------- ...pe=ObjectId_implementations=0.verified.txt | 28 +++--------- ...pe=ObjectId_implementations=2.verified.txt | 28 +++--------- ...pe=ObjectId_implementations=4.verified.txt | 28 +++--------- ...pe=ObjectId_implementations=6.verified.txt | 28 +++--------- ...pe=ObjectId_implementations=0.verified.txt | 40 ++++------------- ...pe=ObjectId_implementations=2.verified.txt | 40 ++++------------- ...pe=ObjectId_implementations=4.verified.txt | 40 ++++------------- ...pe=ObjectId_implementations=6.verified.txt | 40 ++++------------- ...pe=ObjectId_implementations=0.verified.txt | 40 ++++------------- ...pe=ObjectId_implementations=2.verified.txt | 40 ++++------------- ...pe=ObjectId_implementations=4.verified.txt | 40 ++++------------- ...pe=ObjectId_implementations=6.verified.txt | 40 ++++------------- ...pe=ObjectId_implementations=0.verified.txt | 31 +++---------- ...pe=ObjectId_implementations=2.verified.txt | 31 +++---------- ...pe=ObjectId_implementations=4.verified.txt | 31 +++---------- ...pe=ObjectId_implementations=6.verified.txt | 31 +++---------- ...pe=ObjectId_implementations=0.verified.txt | 31 +++---------- ...pe=ObjectId_implementations=2.verified.txt | 31 +++---------- ...pe=ObjectId_implementations=4.verified.txt | 31 +++---------- ...pe=ObjectId_implementations=6.verified.txt | 31 +++---------- ...pe=ObjectId_implementations=0.verified.txt | 43 +++++-------------- ...pe=ObjectId_implementations=2.verified.txt | 43 +++++-------------- ...pe=ObjectId_implementations=4.verified.txt | 43 +++++-------------- ...pe=ObjectId_implementations=6.verified.txt | 43 +++++-------------- ...pe=ObjectId_implementations=0.verified.txt | 43 +++++-------------- ...pe=ObjectId_implementations=2.verified.txt | 43 +++++-------------- ...pe=ObjectId_implementations=4.verified.txt | 43 +++++-------------- ...pe=ObjectId_implementations=6.verified.txt | 43 +++++-------------- ...pe=ObjectId_implementations=0.verified.txt | 17 +++----- ...pe=ObjectId_implementations=2.verified.txt | 17 +++----- ...pe=ObjectId_implementations=4.verified.txt | 17 +++----- ...pe=ObjectId_implementations=6.verified.txt | 17 +++----- ...pe=ObjectId_implementations=0.verified.txt | 17 +++----- ...pe=ObjectId_implementations=2.verified.txt | 17 +++----- ...pe=ObjectId_implementations=4.verified.txt | 17 +++----- ...pe=ObjectId_implementations=6.verified.txt | 17 +++----- ...pe=ObjectId_implementations=0.verified.txt | 6 +-- ...pe=ObjectId_implementations=2.verified.txt | 6 +-- ...pe=ObjectId_implementations=4.verified.txt | 6 +-- ...pe=ObjectId_implementations=6.verified.txt | 6 +-- ...pe=ObjectId_implementations=0.verified.txt | 6 +-- ...pe=ObjectId_implementations=2.verified.txt | 6 +-- ...pe=ObjectId_implementations=4.verified.txt | 6 +-- ...pe=ObjectId_implementations=6.verified.txt | 6 +-- ...pe=ObjectId_implementations=0.verified.txt | 18 +++----- ...pe=ObjectId_implementations=2.verified.txt | 18 +++----- ...pe=ObjectId_implementations=4.verified.txt | 18 +++----- ...pe=ObjectId_implementations=6.verified.txt | 18 +++----- ...pe=ObjectId_implementations=0.verified.txt | 18 +++----- ...pe=ObjectId_implementations=2.verified.txt | 18 +++----- ...pe=ObjectId_implementations=4.verified.txt | 18 +++----- ...pe=ObjectId_implementations=6.verified.txt | 18 +++----- ...pe=ObjectId_implementations=0.verified.txt | 9 ++-- ...pe=ObjectId_implementations=2.verified.txt | 9 ++-- ...pe=ObjectId_implementations=4.verified.txt | 9 ++-- ...pe=ObjectId_implementations=6.verified.txt | 9 ++-- ...pe=ObjectId_implementations=0.verified.txt | 9 ++-- ...pe=ObjectId_implementations=2.verified.txt | 9 ++-- ...pe=ObjectId_implementations=4.verified.txt | 9 ++-- ...pe=ObjectId_implementations=6.verified.txt | 9 ++-- ...pe=ObjectId_implementations=0.verified.txt | 21 +++------ ...pe=ObjectId_implementations=2.verified.txt | 21 +++------ ...pe=ObjectId_implementations=4.verified.txt | 21 +++------ ...pe=ObjectId_implementations=6.verified.txt | 21 +++------ ...pe=ObjectId_implementations=0.verified.txt | 21 +++------ ...pe=ObjectId_implementations=2.verified.txt | 21 +++------ ...pe=ObjectId_implementations=4.verified.txt | 21 +++------ ...pe=ObjectId_implementations=6.verified.txt | 21 +++------ ...pe=ObjectId_implementations=0.verified.txt | 3 +- ...pe=ObjectId_implementations=2.verified.txt | 3 +- ...pe=ObjectId_implementations=4.verified.txt | 3 +- ...pe=ObjectId_implementations=6.verified.txt | 3 +- ...pe=ObjectId_implementations=0.verified.txt | 3 +- ...pe=ObjectId_implementations=2.verified.txt | 3 +- ...pe=ObjectId_implementations=4.verified.txt | 3 +- ...pe=ObjectId_implementations=6.verified.txt | 3 +- ...pe=ObjectId_implementations=0.verified.txt | 15 ++----- ...pe=ObjectId_implementations=2.verified.txt | 15 ++----- ...pe=ObjectId_implementations=4.verified.txt | 15 ++----- ...pe=ObjectId_implementations=6.verified.txt | 15 ++----- ...pe=ObjectId_implementations=0.verified.txt | 15 ++----- ...pe=ObjectId_implementations=2.verified.txt | 15 ++----- ...pe=ObjectId_implementations=4.verified.txt | 15 ++----- ...pe=ObjectId_implementations=6.verified.txt | 15 ++----- ...pe=ObjectId_implementations=0.verified.txt | 6 +-- ...pe=ObjectId_implementations=2.verified.txt | 6 +-- ...pe=ObjectId_implementations=4.verified.txt | 6 +-- ...pe=ObjectId_implementations=6.verified.txt | 6 +-- ...pe=ObjectId_implementations=0.verified.txt | 6 +-- ...pe=ObjectId_implementations=2.verified.txt | 6 +-- ...pe=ObjectId_implementations=4.verified.txt | 6 +-- ...pe=ObjectId_implementations=6.verified.txt | 6 +-- ...pe=ObjectId_implementations=0.verified.txt | 18 +++----- ...pe=ObjectId_implementations=2.verified.txt | 18 +++----- ...pe=ObjectId_implementations=4.verified.txt | 18 +++----- ...pe=ObjectId_implementations=6.verified.txt | 18 +++----- ...pe=ObjectId_implementations=0.verified.txt | 18 +++----- ...pe=ObjectId_implementations=2.verified.txt | 18 +++----- ...pe=ObjectId_implementations=4.verified.txt | 18 +++----- ...pe=ObjectId_implementations=6.verified.txt | 18 +++----- ...pe=ObjectId_implementations=0.verified.txt | 7 ++- ...pe=ObjectId_implementations=2.verified.txt | 7 ++- ...pe=ObjectId_implementations=4.verified.txt | 7 ++- ...pe=ObjectId_implementations=6.verified.txt | 7 ++- ...pe=ObjectId_implementations=0.verified.txt | 14 ++---- ...pe=ObjectId_implementations=2.verified.txt | 14 ++---- ...pe=ObjectId_implementations=4.verified.txt | 14 ++---- ...pe=ObjectId_implementations=6.verified.txt | 14 ++---- ...pe=ObjectId_implementations=0.verified.txt | 7 ++- ...pe=ObjectId_implementations=2.verified.txt | 7 ++- ...pe=ObjectId_implementations=4.verified.txt | 7 ++- ...pe=ObjectId_implementations=6.verified.txt | 7 ++- ...pe=ObjectId_implementations=0.verified.txt | 19 +++----- ...pe=ObjectId_implementations=2.verified.txt | 19 +++----- ...pe=ObjectId_implementations=4.verified.txt | 19 +++----- ...pe=ObjectId_implementations=6.verified.txt | 19 +++----- ...pe=ObjectId_implementations=0.verified.txt | 19 +++----- ...pe=ObjectId_implementations=2.verified.txt | 19 +++----- ...pe=ObjectId_implementations=4.verified.txt | 19 +++----- ...pe=ObjectId_implementations=6.verified.txt | 19 +++----- ...pe=ObjectId_implementations=0.verified.txt | 10 ++--- ...pe=ObjectId_implementations=2.verified.txt | 10 ++--- ...pe=ObjectId_implementations=4.verified.txt | 10 ++--- ...pe=ObjectId_implementations=6.verified.txt | 10 ++--- ...pe=ObjectId_implementations=0.verified.txt | 10 ++--- ...pe=ObjectId_implementations=2.verified.txt | 10 ++--- ...pe=ObjectId_implementations=4.verified.txt | 10 ++--- ...pe=ObjectId_implementations=6.verified.txt | 10 ++--- ...pe=ObjectId_implementations=0.verified.txt | 22 +++------- ...pe=ObjectId_implementations=2.verified.txt | 22 +++------- ...pe=ObjectId_implementations=4.verified.txt | 22 +++------- ...pe=ObjectId_implementations=6.verified.txt | 22 +++------- ...pe=ObjectId_implementations=0.verified.txt | 22 +++------- ...pe=ObjectId_implementations=2.verified.txt | 22 +++------- ...pe=ObjectId_implementations=4.verified.txt | 22 +++------- ...pe=ObjectId_implementations=6.verified.txt | 22 +++------- ...pe=ObjectId_implementations=0.verified.txt | 23 ++-------- ...pe=ObjectId_implementations=2.verified.txt | 23 ++-------- ...pe=ObjectId_implementations=4.verified.txt | 23 ++-------- ...pe=ObjectId_implementations=6.verified.txt | 23 ++-------- ...pe=ObjectId_implementations=0.verified.txt | 23 ++-------- ...pe=ObjectId_implementations=2.verified.txt | 23 ++-------- ...pe=ObjectId_implementations=4.verified.txt | 23 ++-------- ...pe=ObjectId_implementations=6.verified.txt | 23 ++-------- ...pe=ObjectId_implementations=0.verified.txt | 35 +++------------ ...pe=ObjectId_implementations=2.verified.txt | 35 +++------------ ...pe=ObjectId_implementations=4.verified.txt | 35 +++------------ ...pe=ObjectId_implementations=6.verified.txt | 35 +++------------ ...pe=ObjectId_implementations=0.verified.txt | 14 ++---- ...pe=ObjectId_implementations=2.verified.txt | 14 ++---- ...pe=ObjectId_implementations=4.verified.txt | 14 ++---- ...pe=ObjectId_implementations=6.verified.txt | 14 ++---- ...pe=ObjectId_implementations=0.verified.txt | 35 +++------------ ...pe=ObjectId_implementations=2.verified.txt | 35 +++------------ ...pe=ObjectId_implementations=4.verified.txt | 35 +++------------ ...pe=ObjectId_implementations=6.verified.txt | 35 +++------------ ...pe=ObjectId_implementations=0.verified.txt | 26 +++-------- ...pe=ObjectId_implementations=2.verified.txt | 26 +++-------- ...pe=ObjectId_implementations=4.verified.txt | 26 +++-------- ...pe=ObjectId_implementations=6.verified.txt | 26 +++-------- ...pe=ObjectId_implementations=0.verified.txt | 26 +++-------- ...pe=ObjectId_implementations=2.verified.txt | 26 +++-------- ...pe=ObjectId_implementations=4.verified.txt | 26 +++-------- ...pe=ObjectId_implementations=6.verified.txt | 26 +++-------- ...pe=ObjectId_implementations=0.verified.txt | 38 ++++------------ ...pe=ObjectId_implementations=2.verified.txt | 38 ++++------------ ...pe=ObjectId_implementations=4.verified.txt | 38 ++++------------ ...pe=ObjectId_implementations=6.verified.txt | 38 ++++------------ ...pe=ObjectId_implementations=0.verified.txt | 38 ++++------------ ...pe=ObjectId_implementations=2.verified.txt | 38 ++++------------ ...pe=ObjectId_implementations=4.verified.txt | 38 ++++------------ ...pe=ObjectId_implementations=6.verified.txt | 38 ++++------------ ...pe=ObjectId_implementations=0.verified.txt | 27 +++--------- ...pe=ObjectId_implementations=2.verified.txt | 27 +++--------- ...pe=ObjectId_implementations=4.verified.txt | 27 +++--------- ...pe=ObjectId_implementations=6.verified.txt | 27 +++--------- ...pe=ObjectId_implementations=0.verified.txt | 27 +++--------- ...pe=ObjectId_implementations=2.verified.txt | 27 +++--------- ...pe=ObjectId_implementations=4.verified.txt | 27 +++--------- ...pe=ObjectId_implementations=6.verified.txt | 27 +++--------- ...pe=ObjectId_implementations=0.verified.txt | 39 ++++------------- ...pe=ObjectId_implementations=2.verified.txt | 39 ++++------------- ...pe=ObjectId_implementations=4.verified.txt | 39 ++++------------- ...pe=ObjectId_implementations=6.verified.txt | 39 ++++------------- ...pe=ObjectId_implementations=0.verified.txt | 39 ++++------------- ...pe=ObjectId_implementations=2.verified.txt | 39 ++++------------- ...pe=ObjectId_implementations=4.verified.txt | 39 ++++------------- ...pe=ObjectId_implementations=6.verified.txt | 39 ++++------------- ...pe=ObjectId_implementations=0.verified.txt | 30 +++---------- ...pe=ObjectId_implementations=2.verified.txt | 30 +++---------- ...pe=ObjectId_implementations=4.verified.txt | 30 +++---------- ...pe=ObjectId_implementations=6.verified.txt | 30 +++---------- ...pe=ObjectId_implementations=0.verified.txt | 5 +-- ...pe=ObjectId_implementations=2.verified.txt | 5 +-- ...pe=ObjectId_implementations=4.verified.txt | 5 +-- ...pe=ObjectId_implementations=6.verified.txt | 5 +-- ...pe=ObjectId_implementations=0.verified.txt | 30 +++---------- ...pe=ObjectId_implementations=2.verified.txt | 30 +++---------- ...pe=ObjectId_implementations=4.verified.txt | 30 +++---------- ...pe=ObjectId_implementations=6.verified.txt | 30 +++---------- ...pe=ObjectId_implementations=0.verified.txt | 42 +++++------------- ...pe=ObjectId_implementations=2.verified.txt | 42 +++++------------- ...pe=ObjectId_implementations=4.verified.txt | 42 +++++------------- ...pe=ObjectId_implementations=6.verified.txt | 42 +++++------------- ...pe=ObjectId_implementations=0.verified.txt | 42 +++++------------- ...pe=ObjectId_implementations=2.verified.txt | 42 +++++------------- ...pe=ObjectId_implementations=4.verified.txt | 42 +++++------------- ...pe=ObjectId_implementations=6.verified.txt | 42 +++++------------- ...pe=ObjectId_implementations=0.verified.txt | 24 ++--------- ...pe=ObjectId_implementations=2.verified.txt | 24 ++--------- ...pe=ObjectId_implementations=4.verified.txt | 24 ++--------- ...pe=ObjectId_implementations=6.verified.txt | 24 ++--------- ...pe=ObjectId_implementations=0.verified.txt | 24 ++--------- ...pe=ObjectId_implementations=2.verified.txt | 24 ++--------- ...pe=ObjectId_implementations=4.verified.txt | 24 ++--------- ...pe=ObjectId_implementations=6.verified.txt | 24 ++--------- ...pe=ObjectId_implementations=0.verified.txt | 36 +++------------- ...pe=ObjectId_implementations=2.verified.txt | 36 +++------------- ...pe=ObjectId_implementations=4.verified.txt | 36 +++------------- ...pe=ObjectId_implementations=6.verified.txt | 36 +++------------- ...pe=ObjectId_implementations=0.verified.txt | 36 +++------------- ...pe=ObjectId_implementations=2.verified.txt | 36 +++------------- ...pe=ObjectId_implementations=4.verified.txt | 36 +++------------- ...pe=ObjectId_implementations=6.verified.txt | 36 +++------------- ...pe=ObjectId_implementations=0.verified.txt | 27 +++--------- ...pe=ObjectId_implementations=2.verified.txt | 27 +++--------- ...pe=ObjectId_implementations=4.verified.txt | 27 +++--------- ...pe=ObjectId_implementations=6.verified.txt | 27 +++--------- ...pe=ObjectId_implementations=0.verified.txt | 27 +++--------- ...pe=ObjectId_implementations=2.verified.txt | 27 +++--------- ...pe=ObjectId_implementations=4.verified.txt | 27 +++--------- ...pe=ObjectId_implementations=6.verified.txt | 27 +++--------- ...pe=ObjectId_implementations=0.verified.txt | 39 ++++------------- ...pe=ObjectId_implementations=2.verified.txt | 39 ++++------------- ...pe=ObjectId_implementations=4.verified.txt | 39 ++++------------- ...pe=ObjectId_implementations=6.verified.txt | 39 ++++------------- ...pe=ObjectId_implementations=0.verified.txt | 5 +-- ...pe=ObjectId_implementations=2.verified.txt | 5 +-- ...pe=ObjectId_implementations=4.verified.txt | 5 +-- ...pe=ObjectId_implementations=6.verified.txt | 5 +-- ...pe=ObjectId_implementations=0.verified.txt | 39 ++++------------- ...pe=ObjectId_implementations=2.verified.txt | 39 ++++------------- ...pe=ObjectId_implementations=4.verified.txt | 39 ++++------------- ...pe=ObjectId_implementations=6.verified.txt | 39 ++++------------- ...pe=ObjectId_implementations=0.verified.txt | 28 +++--------- ...pe=ObjectId_implementations=2.verified.txt | 28 +++--------- ...pe=ObjectId_implementations=4.verified.txt | 28 +++--------- ...pe=ObjectId_implementations=6.verified.txt | 28 +++--------- ...pe=ObjectId_implementations=0.verified.txt | 28 +++--------- ...pe=ObjectId_implementations=2.verified.txt | 28 +++--------- ...pe=ObjectId_implementations=4.verified.txt | 28 +++--------- ...pe=ObjectId_implementations=6.verified.txt | 28 +++--------- ...pe=ObjectId_implementations=0.verified.txt | 40 ++++------------- ...pe=ObjectId_implementations=2.verified.txt | 40 ++++------------- ...pe=ObjectId_implementations=4.verified.txt | 40 ++++------------- ...pe=ObjectId_implementations=6.verified.txt | 40 ++++------------- ...pe=ObjectId_implementations=0.verified.txt | 40 ++++------------- ...pe=ObjectId_implementations=2.verified.txt | 40 ++++------------- ...pe=ObjectId_implementations=4.verified.txt | 40 ++++------------- ...pe=ObjectId_implementations=6.verified.txt | 40 ++++------------- ...pe=ObjectId_implementations=0.verified.txt | 31 +++---------- ...pe=ObjectId_implementations=2.verified.txt | 31 +++---------- ...pe=ObjectId_implementations=4.verified.txt | 31 +++---------- ...pe=ObjectId_implementations=6.verified.txt | 31 +++---------- ...pe=ObjectId_implementations=0.verified.txt | 31 +++---------- ...pe=ObjectId_implementations=2.verified.txt | 31 +++---------- ...pe=ObjectId_implementations=4.verified.txt | 31 +++---------- ...pe=ObjectId_implementations=6.verified.txt | 31 +++---------- ...pe=ObjectId_implementations=0.verified.txt | 43 +++++-------------- ...pe=ObjectId_implementations=2.verified.txt | 43 +++++-------------- ...pe=ObjectId_implementations=4.verified.txt | 43 +++++-------------- ...pe=ObjectId_implementations=6.verified.txt | 43 +++++-------------- ...pe=ObjectId_implementations=0.verified.txt | 43 +++++-------------- ...pe=ObjectId_implementations=2.verified.txt | 43 +++++-------------- ...pe=ObjectId_implementations=4.verified.txt | 43 +++++-------------- ...pe=ObjectId_implementations=6.verified.txt | 43 +++++-------------- ...pe=ObjectId_implementations=0.verified.txt | 17 +++----- ...pe=ObjectId_implementations=2.verified.txt | 17 +++----- ...pe=ObjectId_implementations=4.verified.txt | 17 +++----- ...pe=ObjectId_implementations=6.verified.txt | 17 +++----- ...pe=ObjectId_implementations=0.verified.txt | 17 +++----- ...pe=ObjectId_implementations=2.verified.txt | 17 +++----- ...pe=ObjectId_implementations=4.verified.txt | 17 +++----- ...pe=ObjectId_implementations=6.verified.txt | 17 +++----- ...pe=ObjectId_implementations=0.verified.txt | 6 +-- ...pe=ObjectId_implementations=2.verified.txt | 6 +-- ...pe=ObjectId_implementations=4.verified.txt | 6 +-- ...pe=ObjectId_implementations=6.verified.txt | 6 +-- ...pe=ObjectId_implementations=0.verified.txt | 6 +-- ...pe=ObjectId_implementations=2.verified.txt | 6 +-- ...pe=ObjectId_implementations=4.verified.txt | 6 +-- ...pe=ObjectId_implementations=6.verified.txt | 6 +-- ...pe=ObjectId_implementations=0.verified.txt | 18 +++----- ...pe=ObjectId_implementations=2.verified.txt | 18 +++----- ...pe=ObjectId_implementations=4.verified.txt | 18 +++----- ...pe=ObjectId_implementations=6.verified.txt | 18 +++----- ...pe=ObjectId_implementations=0.verified.txt | 18 +++----- ...pe=ObjectId_implementations=2.verified.txt | 18 +++----- ...pe=ObjectId_implementations=4.verified.txt | 18 +++----- ...pe=ObjectId_implementations=6.verified.txt | 18 +++----- ...pe=ObjectId_implementations=0.verified.txt | 9 ++-- ...pe=ObjectId_implementations=2.verified.txt | 9 ++-- ...pe=ObjectId_implementations=4.verified.txt | 9 ++-- ...pe=ObjectId_implementations=6.verified.txt | 9 ++-- ...pe=ObjectId_implementations=0.verified.txt | 9 ++-- ...pe=ObjectId_implementations=2.verified.txt | 9 ++-- ...pe=ObjectId_implementations=4.verified.txt | 9 ++-- ...pe=ObjectId_implementations=6.verified.txt | 9 ++-- ...pe=ObjectId_implementations=0.verified.txt | 21 +++------ ...pe=ObjectId_implementations=2.verified.txt | 21 +++------ ...pe=ObjectId_implementations=4.verified.txt | 21 +++------ ...pe=ObjectId_implementations=6.verified.txt | 21 +++------ ...pe=ObjectId_implementations=0.verified.txt | 21 +++------ ...pe=ObjectId_implementations=2.verified.txt | 21 +++------ ...pe=ObjectId_implementations=4.verified.txt | 21 +++------ ...pe=ObjectId_implementations=6.verified.txt | 21 +++------ ...pe=ObjectId_implementations=0.verified.txt | 3 +- ...pe=ObjectId_implementations=2.verified.txt | 3 +- ...pe=ObjectId_implementations=4.verified.txt | 3 +- ...pe=ObjectId_implementations=6.verified.txt | 3 +- ...pe=ObjectId_implementations=0.verified.txt | 3 +- ...pe=ObjectId_implementations=2.verified.txt | 3 +- ...pe=ObjectId_implementations=4.verified.txt | 3 +- ...pe=ObjectId_implementations=6.verified.txt | 3 +- ...pe=ObjectId_implementations=0.verified.txt | 15 ++----- ...pe=ObjectId_implementations=2.verified.txt | 15 ++----- ...pe=ObjectId_implementations=4.verified.txt | 15 ++----- ...pe=ObjectId_implementations=6.verified.txt | 15 ++----- ...pe=ObjectId_implementations=0.verified.txt | 15 ++----- ...pe=ObjectId_implementations=2.verified.txt | 15 ++----- ...pe=ObjectId_implementations=4.verified.txt | 15 ++----- ...pe=ObjectId_implementations=6.verified.txt | 15 ++----- ...pe=ObjectId_implementations=0.verified.txt | 6 +-- ...pe=ObjectId_implementations=2.verified.txt | 6 +-- ...pe=ObjectId_implementations=4.verified.txt | 6 +-- ...pe=ObjectId_implementations=6.verified.txt | 6 +-- ...pe=ObjectId_implementations=0.verified.txt | 6 +-- ...pe=ObjectId_implementations=2.verified.txt | 6 +-- ...pe=ObjectId_implementations=4.verified.txt | 6 +-- ...pe=ObjectId_implementations=6.verified.txt | 6 +-- ...pe=ObjectId_implementations=0.verified.txt | 18 +++----- ...pe=ObjectId_implementations=2.verified.txt | 18 +++----- ...pe=ObjectId_implementations=4.verified.txt | 18 +++----- ...pe=ObjectId_implementations=6.verified.txt | 18 +++----- ...pe=ObjectId_implementations=0.verified.txt | 18 +++----- ...pe=ObjectId_implementations=2.verified.txt | 18 +++----- ...pe=ObjectId_implementations=4.verified.txt | 18 +++----- ...pe=ObjectId_implementations=6.verified.txt | 18 +++----- ...pe=ObjectId_implementations=0.verified.txt | 7 ++- ...pe=ObjectId_implementations=2.verified.txt | 7 ++- ...pe=ObjectId_implementations=4.verified.txt | 7 ++- ...pe=ObjectId_implementations=6.verified.txt | 7 ++- ...pe=ObjectId_implementations=0.verified.txt | 14 ++---- ...pe=ObjectId_implementations=2.verified.txt | 14 ++---- ...pe=ObjectId_implementations=4.verified.txt | 14 ++---- ...pe=ObjectId_implementations=6.verified.txt | 14 ++---- ...pe=ObjectId_implementations=0.verified.txt | 7 ++- ...pe=ObjectId_implementations=2.verified.txt | 7 ++- ...pe=ObjectId_implementations=4.verified.txt | 7 ++- ...pe=ObjectId_implementations=6.verified.txt | 7 ++- ...pe=ObjectId_implementations=0.verified.txt | 19 +++----- ...pe=ObjectId_implementations=2.verified.txt | 19 +++----- ...pe=ObjectId_implementations=4.verified.txt | 19 +++----- ...pe=ObjectId_implementations=6.verified.txt | 19 +++----- ...pe=ObjectId_implementations=0.verified.txt | 19 +++----- ...pe=ObjectId_implementations=2.verified.txt | 19 +++----- ...pe=ObjectId_implementations=4.verified.txt | 19 +++----- ...pe=ObjectId_implementations=6.verified.txt | 19 +++----- ...pe=ObjectId_implementations=0.verified.txt | 10 ++--- ...pe=ObjectId_implementations=2.verified.txt | 10 ++--- ...pe=ObjectId_implementations=4.verified.txt | 10 ++--- ...pe=ObjectId_implementations=6.verified.txt | 10 ++--- ...pe=ObjectId_implementations=0.verified.txt | 10 ++--- ...pe=ObjectId_implementations=2.verified.txt | 10 ++--- ...pe=ObjectId_implementations=4.verified.txt | 10 ++--- ...pe=ObjectId_implementations=6.verified.txt | 10 ++--- ...pe=ObjectId_implementations=0.verified.txt | 22 +++------- ...pe=ObjectId_implementations=2.verified.txt | 22 +++------- ...pe=ObjectId_implementations=4.verified.txt | 22 +++------- ...pe=ObjectId_implementations=6.verified.txt | 22 +++------- ...pe=ObjectId_implementations=0.verified.txt | 22 +++------- ...pe=ObjectId_implementations=2.verified.txt | 22 +++------- ...pe=ObjectId_implementations=4.verified.txt | 22 +++------- ...pe=ObjectId_implementations=6.verified.txt | 22 +++------- ...pe=ObjectId_implementations=0.verified.txt | 23 ++-------- ...pe=ObjectId_implementations=2.verified.txt | 23 ++-------- ...pe=ObjectId_implementations=4.verified.txt | 23 ++-------- ...pe=ObjectId_implementations=6.verified.txt | 23 ++-------- ...pe=ObjectId_implementations=0.verified.txt | 23 ++-------- ...pe=ObjectId_implementations=2.verified.txt | 23 ++-------- ...pe=ObjectId_implementations=4.verified.txt | 23 ++-------- ...pe=ObjectId_implementations=6.verified.txt | 23 ++-------- ...pe=ObjectId_implementations=0.verified.txt | 35 +++------------ ...pe=ObjectId_implementations=2.verified.txt | 35 +++------------ ...pe=ObjectId_implementations=4.verified.txt | 35 +++------------ ...pe=ObjectId_implementations=6.verified.txt | 35 +++------------ ...pe=ObjectId_implementations=0.verified.txt | 14 ++---- ...pe=ObjectId_implementations=2.verified.txt | 14 ++---- ...pe=ObjectId_implementations=4.verified.txt | 14 ++---- ...pe=ObjectId_implementations=6.verified.txt | 14 ++---- ...pe=ObjectId_implementations=0.verified.txt | 35 +++------------ ...pe=ObjectId_implementations=2.verified.txt | 35 +++------------ ...pe=ObjectId_implementations=4.verified.txt | 35 +++------------ ...pe=ObjectId_implementations=6.verified.txt | 35 +++------------ ...pe=ObjectId_implementations=0.verified.txt | 26 +++-------- ...pe=ObjectId_implementations=2.verified.txt | 26 +++-------- ...pe=ObjectId_implementations=4.verified.txt | 26 +++-------- ...pe=ObjectId_implementations=6.verified.txt | 26 +++-------- ...pe=ObjectId_implementations=0.verified.txt | 26 +++-------- ...pe=ObjectId_implementations=2.verified.txt | 26 +++-------- ...pe=ObjectId_implementations=4.verified.txt | 26 +++-------- ...pe=ObjectId_implementations=6.verified.txt | 26 +++-------- ...pe=ObjectId_implementations=0.verified.txt | 38 ++++------------ ...pe=ObjectId_implementations=2.verified.txt | 38 ++++------------ ...pe=ObjectId_implementations=4.verified.txt | 38 ++++------------ ...pe=ObjectId_implementations=6.verified.txt | 38 ++++------------ ...pe=ObjectId_implementations=0.verified.txt | 38 ++++------------ ...pe=ObjectId_implementations=2.verified.txt | 38 ++++------------ ...pe=ObjectId_implementations=4.verified.txt | 38 ++++------------ ...pe=ObjectId_implementations=6.verified.txt | 38 ++++------------ ...pe=ObjectId_implementations=0.verified.txt | 27 +++--------- ...pe=ObjectId_implementations=2.verified.txt | 27 +++--------- ...pe=ObjectId_implementations=4.verified.txt | 27 +++--------- ...pe=ObjectId_implementations=6.verified.txt | 27 +++--------- ...pe=ObjectId_implementations=0.verified.txt | 27 +++--------- ...pe=ObjectId_implementations=2.verified.txt | 27 +++--------- ...pe=ObjectId_implementations=4.verified.txt | 27 +++--------- ...pe=ObjectId_implementations=6.verified.txt | 27 +++--------- ...pe=ObjectId_implementations=0.verified.txt | 39 ++++------------- ...pe=ObjectId_implementations=2.verified.txt | 39 ++++------------- ...pe=ObjectId_implementations=4.verified.txt | 39 ++++------------- ...pe=ObjectId_implementations=6.verified.txt | 39 ++++------------- ...pe=ObjectId_implementations=0.verified.txt | 39 ++++------------- ...pe=ObjectId_implementations=2.verified.txt | 39 ++++------------- ...pe=ObjectId_implementations=4.verified.txt | 39 ++++------------- ...pe=ObjectId_implementations=6.verified.txt | 39 ++++------------- ...pe=ObjectId_implementations=0.verified.txt | 30 +++---------- ...pe=ObjectId_implementations=2.verified.txt | 30 +++---------- ...pe=ObjectId_implementations=4.verified.txt | 30 +++---------- ...pe=ObjectId_implementations=6.verified.txt | 30 +++---------- ...pe=ObjectId_implementations=0.verified.txt | 5 +-- ...pe=ObjectId_implementations=2.verified.txt | 5 +-- ...pe=ObjectId_implementations=4.verified.txt | 5 +-- ...pe=ObjectId_implementations=6.verified.txt | 5 +-- ...pe=ObjectId_implementations=0.verified.txt | 30 +++---------- ...pe=ObjectId_implementations=2.verified.txt | 30 +++---------- ...pe=ObjectId_implementations=4.verified.txt | 30 +++---------- ...pe=ObjectId_implementations=6.verified.txt | 30 +++---------- ...pe=ObjectId_implementations=0.verified.txt | 42 +++++------------- ...pe=ObjectId_implementations=2.verified.txt | 42 +++++------------- ...pe=ObjectId_implementations=4.verified.txt | 42 +++++------------- ...pe=ObjectId_implementations=6.verified.txt | 42 +++++------------- ...pe=ObjectId_implementations=0.verified.txt | 42 +++++------------- ...pe=ObjectId_implementations=2.verified.txt | 42 +++++------------- ...pe=ObjectId_implementations=4.verified.txt | 42 +++++------------- ...pe=ObjectId_implementations=6.verified.txt | 42 +++++------------- ...pe=ObjectId_implementations=0.verified.txt | 24 ++--------- ...pe=ObjectId_implementations=2.verified.txt | 24 ++--------- ...pe=ObjectId_implementations=4.verified.txt | 24 ++--------- ...pe=ObjectId_implementations=6.verified.txt | 24 ++--------- ...pe=ObjectId_implementations=0.verified.txt | 24 ++--------- ...pe=ObjectId_implementations=2.verified.txt | 24 ++--------- ...pe=ObjectId_implementations=4.verified.txt | 24 ++--------- ...pe=ObjectId_implementations=6.verified.txt | 24 ++--------- 502 files changed, 2812 insertions(+), 8740 deletions(-) diff --git a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_DapperTypeHandler.cs b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_DapperTypeHandler.cs index 5205a903f..6b0fb4b44 100644 --- a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_DapperTypeHandler.cs +++ b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_DapperTypeHandler.cs @@ -11,7 +11,6 @@ public override TESTID Parse(object value) return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new TESTID(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => TESTID.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to TESTID"), }; } diff --git a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_EfCoreValueConverter.cs b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_EfCoreValueConverter.cs index 556a3c10c..c94a00c6c 100644 --- a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_EfCoreValueConverter.cs +++ b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_EfCoreValueConverter.cs @@ -4,8 +4,8 @@ public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueC public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? TESTID.Empty : new TESTID(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new TESTID(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_MongoSerializer.cs b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_MongoSerializer.cs index 13a403ebc..74bbecaae 100644 --- a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_MongoSerializer.cs +++ b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_MongoSerializer.cs @@ -2,26 +2,11 @@ { public override TESTID Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new TESTID(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return TESTID.Empty; - } + return new TESTID(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, TESTID value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_NewtonsoftJsonConverter.cs b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_NewtonsoftJsonConverter.cs index b5e2c82de..2f19f4495 100644 --- a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_NewtonsoftJsonConverter.cs +++ b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_NewtonsoftJsonConverter.cs @@ -8,19 +8,13 @@ public override bool CanConvert(System.Type objectType) public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is TESTID id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (TESTID)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? TESTID.Empty : new TESTID(new MongoDB.Bson.ObjectId(result)); + return new TESTID(new MongoDB.Bson.ObjectId(result)); } } \ No newline at end of file diff --git a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_SystemTextJsonConverter.cs b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_SystemTextJsonConverter.cs index 9462f2ac0..c09fe18b8 100644 --- a/src/StronglyTypedIds/Templates/ObjectId/ObjectId_SystemTextJsonConverter.cs +++ b/src/StronglyTypedIds/Templates/ObjectId/ObjectId_SystemTextJsonConverter.cs @@ -3,8 +3,7 @@ class TESTIDSystemTextJsonConverter : System.Text.Json.Serialization.JsonConvert { public override TESTID Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? TESTID.Empty : new TESTID(new MongoDB.Bson.ObjectId(result)); + return new TESTID(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, TESTID value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.IntegrationTests/ObjectIdTests.cs b/test/StronglyTypedIds.IntegrationTests/ObjectIdTests.cs index faa471c03..b92981bac 100644 --- a/test/StronglyTypedIds.IntegrationTests/ObjectIdTests.cs +++ b/test/StronglyTypedIds.IntegrationTests/ObjectIdTests.cs @@ -24,7 +24,7 @@ public ObjectIdTests(MongoDbFixture mongoDbFixture) { _mongoDbFixture = mongoDbFixture; } - + [Fact] public void SameValuesAreEqual() { @@ -95,18 +95,6 @@ public void CanSerializeToObjectId_WithTypeConverter() Assert.Equal(serializedFoo, serializedObjectId); } - - [Fact] - public void CanSerializeToNullable_WithNewtonsoftJsonProvider() - { - var entity = new EntityWithNullableId { Id = null }; - - var json = NewtonsoftJsonSerializer.SerializeObject(entity); - var deserialize = NewtonsoftJsonSerializer.DeserializeObject(json); - - Assert.NotNull(deserialize); - Assert.Equal(deserialize.Id, NewtonsoftJsonObjectIdId.Empty); - } [Fact] public void CanSerializeToObjectId_WithSystemTextJsonProvider() @@ -231,7 +219,7 @@ public async Task WhenDapperValueConverterUsesValueConverter() var value = Assert.Single(results); Assert.Equal(new DapperObjectIdId(new ObjectId("62758c6ee39f6ef4751bb831")), value); } - + [Fact] public async Task WhenMongoSerializerUsesSerializer() { @@ -241,7 +229,7 @@ public async Task WhenMongoSerializerUsesSerializer() var original = new TestDocument { Id = new MongoObjectIdId(objectIdValue) }; await collection.InsertOneAsync(original); var retrieved = await collection.Find(x => x.Id == new MongoObjectIdId(objectIdValue)).FirstAsync(); - + Assert.Equal(new MongoObjectIdId(objectIdValue), retrieved.Id); } @@ -375,11 +363,6 @@ public class TestEntity { public EfCoreObjectIdId Id { get; set; } } - - public class EntityWithNullableId - { - public NewtonsoftJsonObjectIdId? Id { get; set; } - } public class TestDocument { diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=0.verified.txt index 797ff736d..9de33416b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -50,7 +50,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -65,47 +64,26 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=2.verified.txt index aa2a25aa9..e1f7eb970 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -50,7 +50,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -65,47 +64,26 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=4.verified.txt index f0910cce6..2a0733d5f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -51,7 +51,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -66,47 +65,26 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=6.verified.txt index 9f98406e2..2375b15b9 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=100_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -51,7 +51,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -66,47 +65,26 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=0.verified.txt index 902b2f2ca..a3c10e182 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -51,7 +51,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -108,47 +107,26 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=2.verified.txt index cbdd8fd5e..0710ccf66 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -51,7 +51,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -108,47 +107,26 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=4.verified.txt index a7195a763..5289626ac 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -52,7 +52,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -109,47 +108,26 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=6.verified.txt index b6043b145..9d2b6226a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=102_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -52,7 +52,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -109,47 +108,26 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=0.verified.txt index eeb63ae90..6897dc51b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -50,7 +50,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -60,8 +59,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -80,27 +78,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=2.verified.txt index 07a701cb7..b04568343 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -50,7 +50,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -60,8 +59,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -80,27 +78,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=4.verified.txt index e9364bf9b..c3d7c4922 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -51,7 +51,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -61,8 +60,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -81,27 +79,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=6.verified.txt index b64aa71a7..f1ba629e9 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=104_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -51,7 +51,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -61,8 +60,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -81,27 +79,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=0.verified.txt index 1dde94e61..6fb0a03bb 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -51,7 +51,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -103,8 +102,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -123,27 +121,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=2.verified.txt index 65c9c269a..dc1cb80b1 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -51,7 +51,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -103,8 +102,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -123,27 +121,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=4.verified.txt index 2a51b1fe1..4e716bbfa 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -52,7 +52,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -104,8 +103,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -124,27 +122,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=6.verified.txt index acae48b83..97dc1da04 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=106_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -52,7 +52,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -104,8 +103,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -124,27 +122,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=0.verified.txt index 80d07093c..918c63c16 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -51,7 +51,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -66,20 +65,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -87,8 +80,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -107,27 +99,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=2.verified.txt index 7e998890d..bd696ad64 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -51,7 +51,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -66,20 +65,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -87,8 +80,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -107,27 +99,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=4.verified.txt index 52eb0b2cd..d73231fc9 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -52,7 +52,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -67,20 +66,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -88,8 +81,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -108,27 +100,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=6.verified.txt index 4c45daf7a..89e57c2e4 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=108_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -52,7 +52,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -67,20 +66,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -88,8 +81,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -108,27 +100,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=0.verified.txt index 1dc0ce08c..838fa3c48 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -84,8 +84,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=2.verified.txt index 0b31b44ef..de21d171d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -84,8 +84,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=4.verified.txt index 4bdba08cf..bfb7bcd25 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -85,8 +85,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=6.verified.txt index fe291f15d..c79b33b8e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=10_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -85,8 +85,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=0.verified.txt index 2e8a6953f..f9a6ab4c3 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -52,7 +52,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -109,20 +108,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -130,8 +123,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -150,27 +142,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=2.verified.txt index 277bd7750..53ba99c84 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -52,7 +52,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -109,20 +108,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -130,8 +123,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -150,27 +142,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=4.verified.txt index e3402db2f..1033ebe2f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -53,7 +53,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -110,20 +109,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -131,8 +124,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -151,27 +143,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=6.verified.txt index 98ea39718..e18324f18 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=110_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -53,7 +53,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -110,20 +109,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -131,8 +124,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -151,27 +143,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=0.verified.txt index 7621033b4..d1427275b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -60,7 +60,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -69,27 +68,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=2.verified.txt index 8e32835a7..ef2fd842b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -60,7 +60,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -69,27 +68,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=4.verified.txt index 3ea7a5658..c3519dc23 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -61,7 +61,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -70,27 +69,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=6.verified.txt index bcfcaad28..8dc0fca01 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=112_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -61,7 +61,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -70,27 +69,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=0.verified.txt index a9406cebe..6d74bad0e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -61,7 +61,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -112,27 +111,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=2.verified.txt index d3d33cc49..823ad3fd7 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -61,7 +61,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -112,27 +111,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=4.verified.txt index 51ac2e0b6..b81526656 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -62,7 +62,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -113,27 +112,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=6.verified.txt index 42881f13e..a11da9425 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=114_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -62,7 +62,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -113,27 +112,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=0.verified.txt index 89d44b942..b5efa629f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -61,7 +61,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -76,47 +75,26 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=2.verified.txt index e0c124bda..8155a8929 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -61,7 +61,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -76,47 +75,26 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=4.verified.txt index b9e0eec34..fc14cc286 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -62,7 +62,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -77,47 +76,26 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=6.verified.txt index 4634f0428..527bc1b3f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=116_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -62,7 +62,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -77,47 +76,26 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=0.verified.txt index fd7a45926..c07b0ec04 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -62,7 +62,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -119,47 +118,26 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=2.verified.txt index d5c032cdc..a3fc613fc 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -62,7 +62,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -119,47 +118,26 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=4.verified.txt index eec74ab0e..9993f2b86 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -45,8 +45,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -63,7 +63,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -120,47 +119,26 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=6.verified.txt index 6c705f3ab..f76e495ce 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=118_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -45,8 +45,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -63,7 +63,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -120,47 +119,26 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=0.verified.txt index bec774c64..9c9da5ef6 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -61,7 +61,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -71,8 +70,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -91,27 +89,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=2.verified.txt index 4e33b967b..5e3f1a013 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -61,7 +61,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -71,8 +70,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -91,27 +89,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=4.verified.txt index 6139410f4..615239406 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -62,7 +62,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -72,8 +71,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -92,27 +90,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=6.verified.txt index a203813f5..694d7325e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=120_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -62,7 +62,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -72,8 +71,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -92,27 +90,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=0.verified.txt index 0a485c6b6..287b4d560 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -62,7 +62,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -114,8 +113,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -134,27 +132,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=2.verified.txt index 73f8b52cc..8e30ea643 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -62,7 +62,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -114,8 +113,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -134,27 +132,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=4.verified.txt index b02568605..1dd7a857d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -45,8 +45,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -63,7 +63,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -115,8 +114,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -135,27 +133,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=6.verified.txt index 173926f1f..149611d61 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=122_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -45,8 +45,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -63,7 +63,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -115,8 +114,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -135,27 +133,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=0.verified.txt index f17a9d0fa..2c2bba973 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -62,7 +62,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -77,20 +76,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -98,8 +91,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -118,27 +110,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=2.verified.txt index d82a2535b..83887ce1e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -62,7 +62,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -77,20 +76,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -98,8 +91,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -118,27 +110,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=4.verified.txt index 3f9df0183..51de0950a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -45,8 +45,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -63,7 +63,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -78,20 +77,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -99,8 +92,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -119,27 +111,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=6.verified.txt index e9087e4a0..95a7fa58b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=124_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -45,8 +45,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -63,7 +63,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -78,20 +77,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -99,8 +92,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -119,27 +111,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=0.verified.txt index 9afaf334e..f34685b2e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -45,8 +45,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -63,7 +63,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -120,20 +119,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -141,8 +134,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -161,27 +153,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=2.verified.txt index 01e9f6f97..158e711b5 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -45,8 +45,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -63,7 +63,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -120,20 +119,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -141,8 +134,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -161,27 +153,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=4.verified.txt index dba3fe231..37ed90c44 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -46,8 +46,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -64,7 +64,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -121,20 +120,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -142,8 +135,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -162,27 +154,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=6.verified.txt index bb0706b5d..7f2c2b906 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=126_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -46,8 +46,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -64,7 +64,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -121,20 +120,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -142,8 +135,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -162,27 +154,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=0.verified.txt index 1a88668ca..0cfaad9e7 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -47,20 +47,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -68,8 +62,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=2.verified.txt index 06b4af719..d086e585f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -47,20 +47,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -68,8 +62,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=4.verified.txt index 65e8549ec..cb48108a1 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -48,20 +48,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -69,8 +63,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=6.verified.txt index b26443411..85c2e6d1b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=12_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -48,20 +48,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -69,8 +63,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=0.verified.txt index 967685186..76d9e57cc 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -90,20 +90,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -111,8 +105,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=2.verified.txt index 5dd4b4798..41170787e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -90,20 +90,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -111,8 +105,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=4.verified.txt index e965baeb8..05bb2e48e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -91,20 +91,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -112,8 +106,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=6.verified.txt index 8f762955c..df7f76e0c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=14_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -91,20 +91,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -112,8 +106,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=0.verified.txt index 50d76bc6c..25b64c0c6 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=2.verified.txt index 28b3bbb52..0cbf963e3 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=4.verified.txt index 21597ff58..baa1fd5fb 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=6.verified.txt index 679226165..a560fca3d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=16_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=0.verified.txt index 772e2d55f..4777fa60d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=2.verified.txt index ef0c4872a..c89a62321 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=4.verified.txt index 211175d3e..e1b446137 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=6.verified.txt index ef5b4fedb..463ac44ea 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=18_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=0.verified.txt index 520d8923f..77406f215 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -57,20 +57,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=2.verified.txt index b168efacc..64ee2c5a3 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -57,20 +57,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=4.verified.txt index dd028c38e..eac04d29a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -58,20 +58,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=6.verified.txt index c4e673b86..db97f4e1c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=20_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -58,20 +58,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=0.verified.txt index b8442c07b..5ff832e55 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -100,20 +100,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=2.verified.txt index 1b98e6062..afc3fb4ed 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -100,20 +100,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=4.verified.txt index 584230a5f..1f1fd022e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -101,20 +101,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=6.verified.txt index 4a825bcbe..191ad9428 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=22_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -101,20 +101,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=0.verified.txt index 4a95ad4f5..4d4357327 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -52,8 +52,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=2.verified.txt index b874d2da7..3b3f67810 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -52,8 +52,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=4.verified.txt index 7507935a4..5d5e18f4d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -53,8 +53,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=6.verified.txt index 3e681f474..fd3bcb76e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=24_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -53,8 +53,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=0.verified.txt index 3e8d29e56..3b6f69084 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -95,8 +95,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=2.verified.txt index 2f0fa6dbc..24db8601f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -95,8 +95,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=4.verified.txt index 94ffca479..a925a4740 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -96,8 +96,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=6.verified.txt index 191b3d14a..e367a0efd 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=26_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -96,8 +96,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=0.verified.txt index 867a06cc3..f27f424c0 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -58,20 +58,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -79,8 +73,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=2.verified.txt index 1510f75c3..9fd734dab 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -58,20 +58,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -79,8 +73,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=4.verified.txt index f46a29ae4..38718f8df 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -59,20 +59,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -80,8 +74,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=6.verified.txt index 13645b411..0193688cc 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=28_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -59,20 +59,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -80,8 +74,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=0.verified.txt index c6d7dae41..eb2c59e4c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -101,20 +101,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -122,8 +116,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=2.verified.txt index e37a85763..c2aa31d96 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -101,20 +101,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -122,8 +116,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=4.verified.txt index 6a0510db5..0542cf27e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -45,8 +45,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -102,20 +102,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -123,8 +117,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=6.verified.txt index d97888a2e..8ad25e78f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=30_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -45,8 +45,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -102,20 +102,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -123,8 +117,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=0.verified.txt index 6c0290055..ff9907b66 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -48,7 +48,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => 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=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=2.verified.txt index 42e869041..1f03d4ff2 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -48,7 +48,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => 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=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=4.verified.txt index 8fd6f46f5..80728fa60 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -49,7 +49,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => 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=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=6.verified.txt index a3a579249..b4263a247 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=32_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -49,7 +49,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => 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=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=0.verified.txt index 111145a83..2528f5d22 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -49,7 +49,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => 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=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=2.verified.txt index 030cd4b60..af1111b7e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -49,7 +49,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => 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=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=4.verified.txt index 565e1f968..6651c07d7 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -50,7 +50,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => 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=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=6.verified.txt index 9278e4ec5..8254716a0 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=34_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -50,7 +50,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => 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=36_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=0.verified.txt index 0748b42af..649ad3155 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -49,7 +49,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -64,20 +63,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=2.verified.txt index 221396511..4b95b1ee9 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -49,7 +49,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -64,20 +63,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=4.verified.txt index ecb9cb32c..fb7a3853f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -50,7 +50,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -65,20 +64,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=6.verified.txt index 43ca11547..6fa2022a0 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=36_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -50,7 +50,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -65,20 +64,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=0.verified.txt index 6986de043..ad26dc344 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -50,7 +50,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -107,20 +106,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=2.verified.txt index dc5c374c0..069aeed1a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -50,7 +50,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -107,20 +106,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=4.verified.txt index f772bcb82..a8273ebf0 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -51,7 +51,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -108,20 +107,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=6.verified.txt index 3185cced3..7a8d90396 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=38_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -51,7 +51,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -108,20 +107,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=0.verified.txt index c7a0a766a..bf7b9566c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -49,7 +49,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -59,8 +58,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=2.verified.txt index 2bdc53793..54b899d05 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -49,7 +49,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -59,8 +58,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=4.verified.txt index 7a4791d4d..0c3f6ce24 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -50,7 +50,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -60,8 +59,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=6.verified.txt index 8e8a2d151..dd2e03830 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=40_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -50,7 +50,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -60,8 +59,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=0.verified.txt index 7110c77cc..92993dc7b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -50,7 +50,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -102,8 +101,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=2.verified.txt index 33183634b..85d5cdf6e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -50,7 +50,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -102,8 +101,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=4.verified.txt index 189c0ec80..ec970a045 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -51,7 +51,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -103,8 +102,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=6.verified.txt index 608cbfd4f..3650357bd 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=42_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -51,7 +51,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -103,8 +102,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=0.verified.txt index 73e1deac8..378f85482 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -50,7 +50,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -65,20 +64,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -86,8 +79,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=2.verified.txt index 40c26da6b..1bf579180 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -50,7 +50,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -65,20 +64,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -86,8 +79,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=4.verified.txt index d7af3eb91..2f7db8c4f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -51,7 +51,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -66,20 +65,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -87,8 +80,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=6.verified.txt index 5516d8532..68a1897e4 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=44_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -51,7 +51,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -66,20 +65,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -87,8 +80,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=0.verified.txt index 29b33ebd9..c9f1f93c6 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -51,7 +51,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -108,20 +107,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -129,8 +122,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=2.verified.txt index fb265f663..a8e25113f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -51,7 +51,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -108,20 +107,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -129,8 +122,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=4.verified.txt index 2793abfa1..26b6a1f8e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -52,7 +52,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -109,20 +108,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -130,8 +123,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=6.verified.txt index b866651fe..4fc68e37b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=46_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -52,7 +52,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -109,20 +108,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -130,8 +123,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=0.verified.txt index b9555daee..c3a9d7be1 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -59,7 +59,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => 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=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=2.verified.txt index 50551a3ed..f9d806084 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -59,7 +59,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => 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=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=4.verified.txt index aff239aa6..7736d789b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -60,7 +60,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => 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=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=6.verified.txt index 0b2704b3b..3a9ed0bfb 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=48_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -60,7 +60,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => 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=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=0.verified.txt index 78d12cba5..b9897802d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -46,20 +46,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=2.verified.txt index fea43dc3e..67c954b5a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -46,20 +46,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=4.verified.txt index 019baffb7..5d7c9b5d9 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -47,20 +47,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=6.verified.txt index 4b942da94..c2981389f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=4_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -47,20 +47,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=0.verified.txt index 1fce9057e..2d1df9b49 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -60,7 +60,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => 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=50_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=2.verified.txt index 4c81fbd6c..151114ecd 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -60,7 +60,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => 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=50_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=4.verified.txt index 87c9003ec..7edddc277 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -61,7 +61,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => 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=50_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=6.verified.txt index 6c7f07352..eb6d2f46b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=50_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -61,7 +61,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => 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=52_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=0.verified.txt index c1a4c30ad..26a4a8667 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -60,7 +60,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -75,20 +74,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=2.verified.txt index 6a00062c4..9e20791d7 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -60,7 +60,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -75,20 +74,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=4.verified.txt index ea363553f..95957d264 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -61,7 +61,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -76,20 +75,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=6.verified.txt index 16e2811f8..7ab2f0711 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=52_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -61,7 +61,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -76,20 +75,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=0.verified.txt index 6592b6e57..9416da427 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -61,7 +61,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -118,20 +117,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=2.verified.txt index d6488d348..be34e5efe 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -61,7 +61,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -118,20 +117,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=4.verified.txt index 565d65f0a..2a033dc38 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -62,7 +62,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -119,20 +118,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=6.verified.txt index 49993ef3c..1dbeb18c5 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=54_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -62,7 +62,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -119,20 +118,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=0.verified.txt index 87a1a6693..a9a085bea 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -60,7 +60,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -70,8 +69,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=2.verified.txt index 3821e15a8..2e2830f16 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -60,7 +60,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -70,8 +69,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=4.verified.txt index c3ddb6a29..817d8fe51 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -61,7 +61,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -71,8 +70,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=6.verified.txt index f683b7d90..bc65edc59 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=56_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -61,7 +61,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -71,8 +70,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=0.verified.txt index 868c5d4a8..3d4153e15 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -61,7 +61,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -113,8 +112,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=2.verified.txt index 50b0e86e7..29352ee19 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -61,7 +61,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -113,8 +112,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=4.verified.txt index bc8d516c9..1537e7231 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -62,7 +62,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -114,8 +113,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=6.verified.txt index 4213cf9a9..33280b7aa 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=58_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -62,7 +62,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -114,8 +113,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=0.verified.txt index 4efdc419f..3b7b3ad22 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -61,7 +61,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -76,20 +75,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -97,8 +90,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=2.verified.txt index 27478fb26..3fc6a5dc1 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -61,7 +61,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -76,20 +75,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -97,8 +90,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=4.verified.txt index 5d5ef9beb..e29bc445f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -62,7 +62,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -77,20 +76,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -98,8 +91,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=6.verified.txt index 3fe2f67fb..81e17404b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=60_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -62,7 +62,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -77,20 +76,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -98,8 +91,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=0.verified.txt index eb25cea56..01a1ea32c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -62,7 +62,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -119,20 +118,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -140,8 +133,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=2.verified.txt index 344e33e6d..d309fd1ad 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -62,7 +62,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -119,20 +118,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -140,8 +133,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=4.verified.txt index 431c141fd..981ee7fd6 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -45,8 +45,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -63,7 +63,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -120,20 +119,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -141,8 +134,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=6.verified.txt index c52f44d88..c3ff041d2 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=62_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -45,8 +45,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -63,7 +63,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -120,20 +119,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -141,8 +134,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=0.verified.txt index b68500092..de28d0984 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -40,27 +40,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=2.verified.txt index 6186e8036..0c92433eb 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -40,27 +40,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=4.verified.txt index ed092191d..be790ded1 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,27 +41,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=6.verified.txt index 06438ed72..b2463bd37 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=64_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,27 +41,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=0.verified.txt index 822719cda..64f7204d6 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -83,27 +83,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=2.verified.txt index 3caf1e945..e6315b6eb 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -83,27 +83,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=4.verified.txt index f32050824..c8c9158f7 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -84,27 +84,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=6.verified.txt index 4eed8dd42..6240a8e5b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=66_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -84,27 +84,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=0.verified.txt index be088fc82..94d522401 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -47,47 +47,26 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=2.verified.txt index 2ff18fa52..b1f0c7325 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -47,47 +47,26 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=4.verified.txt index 718b671f6..6e770f93b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -48,47 +48,26 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=6.verified.txt index 700063d59..9fe8a74ab 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=68_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -48,47 +48,26 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=0.verified.txt index 4292638de..df8b677dd 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -89,20 +89,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=2.verified.txt index f0a5dd3a0..e8bfa9fe0 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -89,20 +89,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=4.verified.txt index eba757f81..3e4fed81b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -90,20 +90,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=6.verified.txt index bd6c3bda0..c028825f2 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=6_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -90,20 +90,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=0.verified.txt index 95c29e60b..e6be4cb9e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -90,47 +90,26 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=2.verified.txt index 527f09ecb..7077f196f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -90,47 +90,26 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=4.verified.txt index 54533dc7e..cd47bb8c7 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -91,47 +91,26 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=6.verified.txt index 9e2825375..0178f35cc 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=70_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -91,47 +91,26 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=0.verified.txt index 868529691..d8669b45a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -62,27 +61,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=2.verified.txt index 5cb21c7bb..c1012e7df 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -62,27 +61,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=4.verified.txt index 36ac776a8..8e983ba74 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -63,27 +62,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=6.verified.txt index 343837c0a..7b2816105 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=72_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -63,27 +62,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=0.verified.txt index 59bb78753..e8bfbf300 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -85,8 +85,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -105,27 +104,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=2.verified.txt index 0687532df..0d179b9a0 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -85,8 +85,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -105,27 +104,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=4.verified.txt index df5bc965b..db957ea0f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -86,8 +86,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -106,27 +105,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=6.verified.txt index 6e408ce6c..4514885f5 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=74_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -86,8 +86,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -106,27 +105,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=0.verified.txt index f98d90086..2430b16bb 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -48,20 +48,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -69,8 +63,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -89,27 +82,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=2.verified.txt index 2d48a13ad..cb88b1383 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -48,20 +48,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -69,8 +63,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -89,27 +82,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=4.verified.txt index 0b9f6d98e..58438889a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -49,20 +49,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -70,8 +64,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -90,27 +83,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=6.verified.txt index ff4850832..dd025fea9 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=76_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -49,20 +49,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -70,8 +64,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -90,27 +83,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=0.verified.txt index b24c74f66..20e54f413 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -91,20 +91,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -112,8 +106,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -132,27 +125,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=2.verified.txt index c9796d743..543ac09c9 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -91,20 +91,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -112,8 +106,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -132,27 +125,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=4.verified.txt index a48952a71..eae4cf27b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -92,20 +92,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -113,8 +107,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -133,27 +126,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=6.verified.txt index ddae4e62e..d3f8b7179 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=78_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -92,20 +92,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -113,8 +107,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -133,27 +126,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=0.verified.txt index cd2558e33..64dacf2d0 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -51,27 +51,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=2.verified.txt index 789be5adc..580b875d6 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -51,27 +51,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=4.verified.txt index bebdd750f..b87e5efde 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -52,27 +52,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=6.verified.txt index 23f29e31c..3188b038d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=80_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -52,27 +52,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=0.verified.txt index 8984bf4c8..6f8db0d4c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -94,27 +94,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=2.verified.txt index 91390a303..8c2b3522d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -94,27 +94,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=4.verified.txt index bce5627ef..0c96b0982 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -95,27 +95,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=6.verified.txt index b4f31f0a6..8dcf6d3ee 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=82_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -95,27 +95,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=0.verified.txt index e600a9375..e90b9c20c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -58,47 +58,26 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=2.verified.txt index 452824c83..43b8ae5ca 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -58,47 +58,26 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=4.verified.txt index fffa6613c..2423b2ac4 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -59,47 +59,26 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=6.verified.txt index 5cdb7a0e0..b32f2f52a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=84_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -59,47 +59,26 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=0.verified.txt index 68d04752b..19305095b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -101,47 +101,26 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=2.verified.txt index ce52e0639..cc2e00109 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -101,47 +101,26 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=4.verified.txt index 268f2e382..e60462742 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -45,8 +45,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -102,47 +102,26 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=6.verified.txt index 518ab7459..a670a1599 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=86_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -45,8 +45,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -102,47 +102,26 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=0.verified.txt index b21981b64..ee434eea6 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -53,8 +53,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -73,27 +72,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=2.verified.txt index ce2f017e0..f4cfde323 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -53,8 +53,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -73,27 +72,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=4.verified.txt index 733cc8406..8388b82be 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -54,8 +54,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -74,27 +73,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=6.verified.txt index 42082210a..1247bfccc 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=88_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -54,8 +54,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -74,27 +73,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=0.verified.txt index e61f5ff6e..2dd5f2d73 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=2.verified.txt index 0bcd7f262..dc75a94cc 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=4.verified.txt index 5f4495750..93edf5031 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=6.verified.txt index 12833a700..83e765ebc 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=8_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=0.verified.txt index 5916ec3f9..8094fce20 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -96,8 +96,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -116,27 +115,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=2.verified.txt index c22c346c0..84d372b6b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -96,8 +96,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -116,27 +115,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=4.verified.txt index dd89afa68..a24ce3cb9 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -45,8 +45,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -97,8 +97,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -117,27 +116,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=6.verified.txt index d17dea51e..88258ebce 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=90_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -45,8 +45,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -97,8 +97,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -117,27 +116,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=0.verified.txt index 703372d91..ec4d81178 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -59,20 +59,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -80,8 +74,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -100,27 +93,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=2.verified.txt index 5205031e7..e2223c42e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -59,20 +59,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -80,8 +74,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -100,27 +93,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=4.verified.txt index 7375671aa..1f2b960c5 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -45,8 +45,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -60,20 +60,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -81,8 +75,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -101,27 +94,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=6.verified.txt index e78d916c5..effce323a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=92_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -45,8 +45,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -60,20 +60,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -81,8 +75,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -101,27 +94,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=0.verified.txt index e32c7d34b..421d57f98 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -45,8 +45,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -102,20 +102,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -123,8 +117,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -143,27 +136,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=2.verified.txt index 8b4cf13bf..42afe77a5 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -45,8 +45,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -102,20 +102,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -123,8 +117,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -143,27 +136,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=4.verified.txt index 730411029..09ad3774f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -46,8 +46,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -103,20 +103,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -124,8 +118,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -144,27 +137,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=6.verified.txt index b8d247392..74dc1a4ae 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=94_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -46,8 +46,8 @@ namespace Some.Namespace public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -103,20 +103,14 @@ namespace Some.Namespace public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -124,8 +118,7 @@ namespace Some.Namespace { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -144,27 +137,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=0.verified.txt index 8dbae2bd5..b0b6f487d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -49,7 +49,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -58,27 +57,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=2.verified.txt index 181a1843e..feb095d3b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -49,7 +49,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -58,27 +57,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=4.verified.txt index 75e119c92..aaf141363 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -50,7 +50,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -59,27 +58,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=6.verified.txt index e6a12720d..4ac31c19f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=96_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -50,7 +50,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -59,27 +58,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=0.verified.txt index 593494065..5c3fdd692 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -50,7 +50,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -101,27 +100,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=2.verified.txt index 2a1c38a75..bdc893e33 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -50,7 +50,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -101,27 +100,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=4.verified.txt index a88950c55..9156d44b7 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -51,7 +51,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -102,27 +101,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=6.verified.txt index 9a2ffd87e..6cc5fdf3f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=Some.Namespace_converter=98_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -51,7 +51,6 @@ namespace Some.Namespace return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -102,27 +101,12 @@ namespace Some.Namespace { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=0.verified.txt index 2aa8286d2..028e051cc 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -48,7 +48,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -63,47 +62,26 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=2.verified.txt index 12cdb21a3..860b69d29 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -48,7 +48,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -63,47 +62,26 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=4.verified.txt index 78a6be997..0b0a5a4d3 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -49,7 +49,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -64,47 +63,26 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=6.verified.txt index 3a7798cec..4b3f229aa 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=100_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -49,7 +49,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -64,47 +63,26 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=0.verified.txt index 18d35bc8c..1aa20da28 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -49,7 +49,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -106,47 +105,26 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=2.verified.txt index 1bacbaa7b..e8a3100ed 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -49,7 +49,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -106,47 +105,26 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=4.verified.txt index a41286e0d..f273679ac 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -50,7 +50,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -107,47 +106,26 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=6.verified.txt index 5c1e0f178..2ccc2bb4c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=102_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -50,7 +50,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -107,47 +106,26 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=0.verified.txt index 9b40e29bd..bbca84b9a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -48,7 +48,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -58,8 +57,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -78,27 +76,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=2.verified.txt index 9978e686f..37261e333 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -48,7 +48,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -58,8 +57,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -78,27 +76,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=4.verified.txt index 60140ae5b..1f52e37e5 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -49,7 +49,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -59,8 +58,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -79,27 +77,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=6.verified.txt index b26f787db..add0a239a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=104_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -49,7 +49,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -59,8 +58,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -79,27 +77,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=0.verified.txt index caa149449..708a16586 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -49,7 +49,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -101,8 +100,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -121,27 +119,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=2.verified.txt index 1af9883c3..228a8ee4f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -49,7 +49,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -101,8 +100,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -121,27 +119,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=4.verified.txt index 2ce1b9aa4..d9d703f38 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -50,7 +50,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -102,8 +101,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -122,27 +120,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=6.verified.txt index f860d0212..2e5de4264 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=106_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -50,7 +50,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -102,8 +101,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -122,27 +120,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=0.verified.txt index 748e15c62..04ee3acda 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -49,7 +49,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -64,20 +63,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -85,8 +78,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -105,27 +97,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=2.verified.txt index 62a77014f..084971bcb 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -49,7 +49,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -64,20 +63,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -85,8 +78,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -105,27 +97,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=4.verified.txt index fda381e52..9cc88675f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -50,7 +50,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -65,20 +64,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -86,8 +79,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -106,27 +98,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=6.verified.txt index 39712c051..9ef04bda0 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=108_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -50,7 +50,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -65,20 +64,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -86,8 +79,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -106,27 +98,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=0.verified.txt index 2c4eb6795..0459ee3ac 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -82,8 +82,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=2.verified.txt index 3f14fc328..7be6cf497 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -82,8 +82,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=4.verified.txt index ff7c33cdb..946437918 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -83,8 +83,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=6.verified.txt index fec485d6a..665f71b41 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=10_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -83,8 +83,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=0.verified.txt index 0a6c42cda..fecf7b8db 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -50,7 +50,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -107,20 +106,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -128,8 +121,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -148,27 +140,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=2.verified.txt index dc1054711..8fa563311 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -50,7 +50,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -107,20 +106,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -128,8 +121,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -148,27 +140,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=4.verified.txt index ec2f3691f..e99d2ba63 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -51,7 +51,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -108,20 +107,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -129,8 +122,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -149,27 +141,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=6.verified.txt index f697e0c9b..71cb9e616 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=110_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -51,7 +51,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -108,20 +107,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -129,8 +122,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -149,27 +141,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=0.verified.txt index b96036480..dcba0a2b6 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -40,8 +40,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -58,7 +58,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -67,27 +66,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=2.verified.txt index 2f1df41fc..8ba791ee2 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -40,8 +40,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -58,7 +58,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -67,27 +66,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=4.verified.txt index 2fb8e553d..872dbbeb3 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -59,7 +59,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -68,27 +67,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=6.verified.txt index 8cd8538e9..bfc5c7330 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=112_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -59,7 +59,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -68,27 +67,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=0.verified.txt index d38a16950..cb1a0662a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -59,7 +59,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -110,27 +109,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=2.verified.txt index 21807a7b9..0fa8042ec 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -59,7 +59,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -110,27 +109,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=4.verified.txt index aaae129b7..8da6d499d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -60,7 +60,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -111,27 +110,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=6.verified.txt index a9f31ed30..b9b0c2a76 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=114_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -60,7 +60,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -111,27 +110,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=0.verified.txt index eb7892945..a126c9c89 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -59,7 +59,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -74,47 +73,26 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=2.verified.txt index 9f1508f9b..b7183402a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -59,7 +59,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -74,47 +73,26 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=4.verified.txt index 2c98c1c09..fad245816 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -60,7 +60,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -75,47 +74,26 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=6.verified.txt index d1fa8ba18..9d667b69e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=116_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -60,7 +60,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -75,47 +74,26 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=0.verified.txt index b4f2ef7a6..2ba9071c1 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -60,7 +60,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -117,47 +116,26 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=2.verified.txt index 8c77b674e..90705fdbb 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -60,7 +60,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -117,47 +116,26 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=4.verified.txt index 91428baec..e3117137f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -61,7 +61,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -118,47 +117,26 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=6.verified.txt index e7ebea21f..63ab5759f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=118_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -61,7 +61,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -118,47 +117,26 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=0.verified.txt index 989320ad3..8e13d6b3d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -59,7 +59,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -69,8 +68,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -89,27 +87,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=2.verified.txt index 4d435c913..5fdf4e1bd 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -59,7 +59,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -69,8 +68,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -89,27 +87,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=4.verified.txt index b65ad4e64..f4c089b5d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -60,7 +60,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -70,8 +69,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -90,27 +88,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=6.verified.txt index 96011e369..2fad44e5a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=120_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -60,7 +60,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -70,8 +69,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -90,27 +88,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=0.verified.txt index 8a23d59a7..26ee02bd9 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -60,7 +60,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -112,8 +111,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -132,27 +130,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=2.verified.txt index 0e1ffb029..bc785d6f4 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -60,7 +60,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -112,8 +111,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -132,27 +130,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=4.verified.txt index 4aeedf808..b60b4f4ac 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -61,7 +61,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -113,8 +112,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -133,27 +131,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=6.verified.txt index 974b64827..82727ac54 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=122_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -61,7 +61,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -113,8 +112,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -133,27 +131,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=0.verified.txt index 6946e230f..284126db4 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -60,7 +60,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -75,20 +74,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -96,8 +89,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -116,27 +108,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=2.verified.txt index 7e6702d29..b6b3a0e53 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -60,7 +60,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -75,20 +74,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -96,8 +89,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -116,27 +108,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=4.verified.txt index 8f67ce49d..7991b1073 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -61,7 +61,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -76,20 +75,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -97,8 +90,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -117,27 +109,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=6.verified.txt index 9bda1971e..485f96dc0 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=124_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -61,7 +61,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -76,20 +75,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -97,8 +90,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -117,27 +109,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=0.verified.txt index c57f2819c..442e923aa 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -61,7 +61,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -118,20 +117,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -139,8 +132,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -159,27 +151,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=2.verified.txt index 239c86080..a841c1a3f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -61,7 +61,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -118,20 +117,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -139,8 +132,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -159,27 +151,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=4.verified.txt index db543a68b..46cdcef8d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -62,7 +62,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -119,20 +118,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -140,8 +133,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -160,27 +152,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=6.verified.txt index e3d192507..aa9788b13 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=126_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -62,7 +62,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -119,20 +118,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -140,8 +133,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -160,27 +152,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=0.verified.txt index 8826bd65b..aff039952 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -45,20 +45,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -66,8 +60,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=2.verified.txt index 3c26bcdd5..e99f03d0e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -45,20 +45,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -66,8 +60,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=4.verified.txt index 1899013b0..ba1dccbe6 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -46,20 +46,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -67,8 +61,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=6.verified.txt index bb91b65bc..673f17487 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=12_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -46,20 +46,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -67,8 +61,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=0.verified.txt index e4687a2d7..905332860 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -88,20 +88,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -109,8 +103,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=2.verified.txt index 2acfc5504..87f93afb6 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -88,20 +88,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -109,8 +103,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=4.verified.txt index 84795d760..740fc6279 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -89,20 +89,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -110,8 +104,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=6.verified.txt index 520bb8c86..5736f9388 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=14_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -89,20 +89,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -110,8 +104,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=0.verified.txt index 946abf89f..39c43972d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -39,8 +39,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=2.verified.txt index 7296ba27c..98c32b4bf 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -39,8 +39,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=4.verified.txt index b47d9a55c..01af38983 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -40,8 +40,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=6.verified.txt index e4375d918..dba76d28e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=16_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -40,8 +40,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=0.verified.txt index 2d4db48cf..c93f736a3 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -40,8 +40,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=2.verified.txt index bf40c7933..1f3efaaea 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -40,8 +40,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=4.verified.txt index d09f1a413..3d2663a2f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=6.verified.txt index 1c3450550..b501d7d95 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=18_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=0.verified.txt index c973dd8b1..83c721d4b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -40,8 +40,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -55,20 +55,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=2.verified.txt index 1c1eb578a..16de86b48 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -40,8 +40,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -55,20 +55,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=4.verified.txt index 502519c68..f24db9d74 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -56,20 +56,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=6.verified.txt index f3b7583b1..2fc9b3ab6 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=20_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -56,20 +56,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=0.verified.txt index 9baf34997..db7c3042c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -98,20 +98,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=2.verified.txt index 2cfe04037..452e05952 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -98,20 +98,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=4.verified.txt index 06be3997c..4ac37003b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -99,20 +99,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=6.verified.txt index af5ca6872..7d1e33834 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=22_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -99,20 +99,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=0.verified.txt index 4833bda84..1ea5af021 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -40,8 +40,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -50,8 +50,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=2.verified.txt index 0265e6eb2..54d009475 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -40,8 +40,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -50,8 +50,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=4.verified.txt index dddf7bdd8..7f9edef90 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -51,8 +51,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=6.verified.txt index 2c469d5fe..4260c7f05 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=24_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -51,8 +51,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=0.verified.txt index e0decd7a2..a204a15af 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -93,8 +93,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=2.verified.txt index 3db33131d..01785edcb 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -93,8 +93,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=4.verified.txt index bcb226936..57dc58471 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -94,8 +94,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=6.verified.txt index 0d7336feb..0059aaee7 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=26_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -94,8 +94,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=0.verified.txt index 500622f19..081c5a3dc 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -56,20 +56,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -77,8 +71,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=2.verified.txt index 0bebc9f0c..36c5763cd 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -56,20 +56,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -77,8 +71,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=4.verified.txt index 80591cbad..abf641be9 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -57,20 +57,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -78,8 +72,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=6.verified.txt index 7bf8266ca..1ccfc6ff6 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=28_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -57,20 +57,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -78,8 +72,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=0.verified.txt index 22578483c..432549ef1 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -99,20 +99,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -120,8 +114,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=2.verified.txt index 613ab38f4..64033ab80 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -99,20 +99,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -120,8 +114,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=4.verified.txt index 3b1ce8b66..c538e1471 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -100,20 +100,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -121,8 +115,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=6.verified.txt index 100b380ac..8326d3004 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=30_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -100,20 +100,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -121,8 +115,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=0.verified.txt index 731a4103f..fef0f55d1 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -46,7 +46,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => 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=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=2.verified.txt index 0992de8e2..373ebb2fe 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -46,7 +46,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => 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=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=4.verified.txt index fb3536d4c..4729f4d3e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -47,7 +47,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => 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=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=6.verified.txt index 9957dc45a..a962711e7 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=32_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -47,7 +47,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => 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=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=0.verified.txt index 492dd3661..ce7ab73d2 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -47,7 +47,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => 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=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=2.verified.txt index 31bc4d663..aed49bcb7 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -47,7 +47,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => 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=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=4.verified.txt index e0e07e5fd..beb10293f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -48,7 +48,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => 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=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=6.verified.txt index f77924cbe..118ce6ed4 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=34_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -48,7 +48,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => 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=36_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=0.verified.txt index fdf28f480..700c61046 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -47,7 +47,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -62,20 +61,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=2.verified.txt index 92671c34e..2a1cfabae 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -47,7 +47,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -62,20 +61,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=4.verified.txt index eca9e8c45..f47b91289 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -48,7 +48,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -63,20 +62,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=6.verified.txt index ce53460fc..63620d035 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=36_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -48,7 +48,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -63,20 +62,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=0.verified.txt index b25657d46..6fdba70a2 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -48,7 +48,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -105,20 +104,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=2.verified.txt index b17bd616b..99143fd27 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -48,7 +48,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -105,20 +104,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=4.verified.txt index 9ec4c7267..f5c22796a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -49,7 +49,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -106,20 +105,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=6.verified.txt index deb6cbd05..984ac9d01 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=38_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -49,7 +49,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -106,20 +105,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=0.verified.txt index e8ac4ce46..9b26ebf19 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -47,7 +47,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -57,8 +56,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=2.verified.txt index 74c91594e..2824546a1 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -47,7 +47,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -57,8 +56,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=4.verified.txt index 9f51081a2..54dbb032e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -48,7 +48,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -58,8 +57,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=6.verified.txt index 8e168c150..8caebd769 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=40_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -48,7 +48,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -58,8 +57,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=0.verified.txt index c74c87448..fbee14859 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -48,7 +48,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -100,8 +99,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=2.verified.txt index 9bf13edd2..c98fbe643 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -48,7 +48,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -100,8 +99,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=4.verified.txt index 9f34eb88f..204e54fef 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -49,7 +49,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -101,8 +100,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=6.verified.txt index b6cae9fae..7e8d13090 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=42_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -49,7 +49,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -101,8 +100,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=0.verified.txt index 99fca607b..3c140deee 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -48,7 +48,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -63,20 +62,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -84,8 +77,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=2.verified.txt index 52385d538..c3be978e7 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -48,7 +48,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -63,20 +62,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -84,8 +77,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=4.verified.txt index 3ebdab09c..89f806a69 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -49,7 +49,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -64,20 +63,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -85,8 +78,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=6.verified.txt index f63aea6f8..36fe36d94 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=44_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -49,7 +49,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -64,20 +63,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -85,8 +78,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=0.verified.txt index 706d1c5e3..7e0b7757e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -49,7 +49,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -106,20 +105,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -127,8 +120,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=2.verified.txt index eb21ddc48..35935bec8 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -49,7 +49,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -106,20 +105,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -127,8 +120,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=4.verified.txt index a6c18fa0d..44e40c0be 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -50,7 +50,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -107,20 +106,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -128,8 +121,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=6.verified.txt index 8ab0011b6..3b19ec422 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=46_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -50,7 +50,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -107,20 +106,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -128,8 +121,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=0.verified.txt index 3a8b6f2ee..724fe51f0 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -39,8 +39,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -57,7 +57,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => 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=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=2.verified.txt index b85c96ad6..8fc09e377 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -39,8 +39,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -57,7 +57,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => 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=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=4.verified.txt index cd5e4c7cb..89c85aa4b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -40,8 +40,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -58,7 +58,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => 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=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=6.verified.txt index 2ef1423c6..3652ff5ca 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=48_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -40,8 +40,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -58,7 +58,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => 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=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=0.verified.txt index 645ad6d26..5227b0005 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,20 +44,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=2.verified.txt index d2f67bb47..2c3f13559 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,20 +44,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=4.verified.txt index b62144637..7dd347e7a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -45,20 +45,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=6.verified.txt index 22cffa42f..89b68c0f6 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=4_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -45,20 +45,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=0.verified.txt index 3b567084f..4702c2587 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -40,8 +40,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -58,7 +58,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => 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=50_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=2.verified.txt index 8d0e418ea..d6bbf9138 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -40,8 +40,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -58,7 +58,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => 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=50_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=4.verified.txt index a4fd418d0..99e1a5f70 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -59,7 +59,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => 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=50_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=6.verified.txt index c9a961c6e..898906c5b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=50_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -59,7 +59,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => 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=52_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=0.verified.txt index 2552d9ee4..c58c64981 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -40,8 +40,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -58,7 +58,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -73,20 +72,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=2.verified.txt index 3dd42d233..6b22228c1 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -40,8 +40,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -58,7 +58,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -73,20 +72,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=4.verified.txt index 23667c56c..0d1f81fd6 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -59,7 +59,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -74,20 +73,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=6.verified.txt index b4e019091..1e05b9e16 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=52_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -59,7 +59,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -74,20 +73,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=0.verified.txt index 74c4b42e2..4c9203b04 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -59,7 +59,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -116,20 +115,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=2.verified.txt index fa6f711fc..d89a922ac 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -59,7 +59,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -116,20 +115,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=4.verified.txt index 5f94a690b..026c3a4ae 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -60,7 +60,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -117,20 +116,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=6.verified.txt index 99a2e8e0b..e7f867a45 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=54_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -60,7 +60,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -117,20 +116,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=0.verified.txt index 7c1825848..97fe148b9 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -40,8 +40,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -58,7 +58,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -68,8 +67,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=2.verified.txt index 7c37d9165..5d8becc0f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -40,8 +40,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -58,7 +58,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -68,8 +67,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=4.verified.txt index 5f9467982..52b6d9901 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -59,7 +59,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -69,8 +68,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=6.verified.txt index 7e8dff87e..c826ef0ba 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=56_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -59,7 +59,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -69,8 +68,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=0.verified.txt index 4d2a69419..6a8abc363 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -59,7 +59,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -111,8 +110,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=2.verified.txt index 2e0ba5a1a..949a68c9d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -59,7 +59,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -111,8 +110,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=4.verified.txt index 4ca6256a7..e979d6962 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -60,7 +60,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -112,8 +111,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=6.verified.txt index c0bf78cbb..b461e3634 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=58_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -60,7 +60,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -112,8 +111,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=0.verified.txt index 7c35bd80a..51f4e3dec 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -59,7 +59,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -74,20 +73,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -95,8 +88,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=2.verified.txt index d9a70faa1..8b8fa8930 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -59,7 +59,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -74,20 +73,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -95,8 +88,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=4.verified.txt index 0d0d676e0..f7f7c7b3a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -60,7 +60,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -75,20 +74,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -96,8 +89,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=6.verified.txt index f8b64eaf3..9ad97c554 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=60_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -60,7 +60,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -75,20 +74,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -96,8 +89,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=0.verified.txt index 8fd9d3fa1..da539f64b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -60,7 +60,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -117,20 +116,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -138,8 +131,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=2.verified.txt index fca5e697d..214fb6bba 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -60,7 +60,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -117,20 +116,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -138,8 +131,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=4.verified.txt index 010eaf01a..0174643f2 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -61,7 +61,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -118,20 +117,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -139,8 +132,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=6.verified.txt index 8ff488730..184947663 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=62_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -61,7 +61,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -118,20 +117,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -139,8 +132,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=0.verified.txt index 95dc624f8..6a7a42014 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,27 +38,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=2.verified.txt index 770b65772..8e515c76f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -38,27 +38,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=4.verified.txt index 43b535812..17ab6c844 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -39,27 +39,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=6.verified.txt index d217ef7c0..d97b427cb 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=64_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -39,27 +39,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=0.verified.txt index 798742c49..50d031aef 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -81,27 +81,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=2.verified.txt index 4aba2cf44..b608d7e93 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -81,27 +81,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=4.verified.txt index 99b6d721e..74ed540bf 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -82,27 +82,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=6.verified.txt index e915acb10..78d4f6ed7 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=66_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -82,27 +82,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=0.verified.txt index 0b1edfa61..e5b227c44 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -45,47 +45,26 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=2.verified.txt index 710a68733..f74b429a1 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -45,47 +45,26 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=4.verified.txt index 173438c75..d0b731b6e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -46,47 +46,26 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=6.verified.txt index e4a3cbaa5..6c3afe17d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=68_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -46,47 +46,26 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=0.verified.txt index 5887eedce..1fcd4cefc 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -87,20 +87,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=2.verified.txt index 8588bf8f2..61a9a873f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -87,20 +87,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=4.verified.txt index 5d8b70573..764ab45d7 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -88,20 +88,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=6.verified.txt index c4c1b13e2..cdeb8f711 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=6_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -88,20 +88,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=0.verified.txt index 4344cf503..2d18183f5 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -88,47 +88,26 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=2.verified.txt index e679429d5..a76e1ed0f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -88,47 +88,26 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=4.verified.txt index d66d1f68a..b4b851bdc 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -89,47 +89,26 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=6.verified.txt index 37b6738db..d70317199 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=70_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -89,47 +89,26 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=0.verified.txt index c2cfe7b4d..21334aecd 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -40,8 +40,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -60,27 +59,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=2.verified.txt index 9ddf04f87..362e4433b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -40,8 +40,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -60,27 +59,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=4.verified.txt index 30fc1b08b..8b2db4df2 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -61,27 +60,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=6.verified.txt index 333f1314c..1372aef55 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=72_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -61,27 +60,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=0.verified.txt index f142ab759..0d979dd91 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -83,8 +83,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -103,27 +102,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=2.verified.txt index add6eb09c..8a17166e1 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -83,8 +83,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -103,27 +102,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=4.verified.txt index 3a33a59d8..09f65872c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -84,8 +84,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -104,27 +103,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=6.verified.txt index be3b10103..c3ae89cf9 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=74_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -84,8 +84,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -104,27 +103,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=0.verified.txt index 66ddeea3c..bdb8a995b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -46,20 +46,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -67,8 +61,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -87,27 +80,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=2.verified.txt index 8c286d71e..2573f8308 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -46,20 +46,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -67,8 +61,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -87,27 +80,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=4.verified.txt index 83b22c256..a7060f252 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -47,20 +47,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -68,8 +62,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -88,27 +81,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=6.verified.txt index f63a43679..3aaa30b53 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=76_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -47,20 +47,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -68,8 +62,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -88,27 +81,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=0.verified.txt index 0b5ab0638..5b3bfc085 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -89,20 +89,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -110,8 +104,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -130,27 +123,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=2.verified.txt index 63cbe303e..ff57bed29 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -89,20 +89,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -110,8 +104,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -130,27 +123,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=4.verified.txt index cf4d42f1d..86d75c7b2 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -90,20 +90,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -111,8 +105,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -131,27 +124,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=6.verified.txt index 86aaa2ec8..57299765d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=78_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -90,20 +90,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -111,8 +105,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -131,27 +124,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=0.verified.txt index 88fb2497a..dbd2348b7 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -40,8 +40,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -49,27 +49,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=2.verified.txt index 4adc3c8b7..a92905074 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -40,8 +40,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -49,27 +49,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=4.verified.txt index f7ea0bd17..64d425d68 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -50,27 +50,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=6.verified.txt index 434688237..15c472d6e 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=80_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -50,27 +50,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=0.verified.txt index b80ede2bb..f4720493d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -92,27 +92,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=2.verified.txt index 30b59df32..e6c323864 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -92,27 +92,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=4.verified.txt index bde10beda..0a9f1ca30 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -93,27 +93,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=6.verified.txt index e16ace8c1..a5aa49f93 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=82_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -93,27 +93,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=0.verified.txt index 0e42ea001..b6df88318 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -56,47 +56,26 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=2.verified.txt index 75677498e..3ff9710c5 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -56,47 +56,26 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=4.verified.txt index e2a2ce096..c630cb228 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -57,47 +57,26 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=6.verified.txt index 741cdff18..66f959927 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=84_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -57,47 +57,26 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=0.verified.txt index 7f59d4411..bb00efd83 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -99,47 +99,26 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=2.verified.txt index b42acdddb..75a5cb067 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -99,47 +99,26 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=4.verified.txt index 9968367aa..e27fd31ab 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -100,47 +100,26 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=6.verified.txt index 66e02b45b..3994726db 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=86_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -100,47 +100,26 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=0.verified.txt index f41489652..430afc3f4 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -51,8 +51,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -71,27 +70,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=2.verified.txt index 25aff466c..67a21a4f2 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -41,8 +41,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -51,8 +51,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -71,27 +70,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=4.verified.txt index 9965dd0fb..ee890f91d 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -52,8 +52,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -72,27 +71,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=6.verified.txt index db503536a..5ae0180cd 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=88_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -52,8 +52,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -72,27 +71,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=0.verified.txt index f68a20a1f..4fe90eb8a 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -39,8 +39,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=2.verified.txt index 622878424..ba22b2046 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -39,8 +39,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=4.verified.txt index b3f510157..9cc982808 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -40,8 +40,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=6.verified.txt index aa9dd6beb..dcf17d801 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=8_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -40,8 +40,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=0.verified.txt index fcf753eba..f7b62fa8c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -94,8 +94,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -114,27 +113,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=2.verified.txt index 393612267..bbe6d1466 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -94,8 +94,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -114,27 +113,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=4.verified.txt index f8cd31588..51e0393ef 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -95,8 +95,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -115,27 +114,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=6.verified.txt index 8b337ccbd..8ce197107 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=90_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -95,8 +95,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -115,27 +114,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=0.verified.txt index 84455b75e..85204b511 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -57,20 +57,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -78,8 +72,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -98,27 +91,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=2.verified.txt index 8a897a132..ba1941541 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -42,8 +42,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -57,20 +57,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -78,8 +72,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -98,27 +91,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=4.verified.txt index 28da71911..df132fc18 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -58,20 +58,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -79,8 +73,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -99,27 +92,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=6.verified.txt index 01eae9492..56b6e11bd 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=92_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -58,20 +58,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -79,8 +73,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -99,27 +92,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=0.verified.txt index 7efc48c3f..b36fd2e43 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -100,20 +100,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -121,8 +115,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -141,27 +134,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=2.verified.txt index fdb3f43eb..769a606bd 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -43,8 +43,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -100,20 +100,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -121,8 +115,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -141,27 +134,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=4.verified.txt index 7b818ce29..3312b7e62 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -101,20 +101,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -122,8 +116,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -142,27 +135,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=6.verified.txt index a0af79484..29f95ebd8 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=94_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -44,8 +44,8 @@ public EfCoreValueConverter() : this(null) { } public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null) : base( - id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(), - value => string.IsNullOrEmpty(value) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(value)), + id => id.Value.ToString(), + value => new MyTestId(new MongoDB.Bson.ObjectId(value)), mappingHints ) { } } @@ -101,20 +101,14 @@ public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer) { - if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty) - { - serializer.Serialize(writer, id.Value.ToString()); - } - else - { - serializer.Serialize(writer, null); - } + var id = (MyTestId)value; + serializer.Serialize(writer, id.Value.ToString()); } public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer) { var result = serializer.Deserialize(reader); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(result)); } } @@ -122,8 +116,7 @@ { public override MyTestId Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options) { - var result = reader.GetString(); - return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result)); + return new MyTestId(new MongoDB.Bson.ObjectId(reader.GetString())); } public override void Write(System.Text.Json.Utf8JsonWriter writer, MyTestId value, System.Text.Json.JsonSerializerOptions options) @@ -142,27 +135,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=0.verified.txt index 2974365f8..035ca9389 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -47,7 +47,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -56,27 +55,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=2.verified.txt index 67f2df0e4..be1043b1c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -47,7 +47,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -56,27 +55,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=4.verified.txt index 1b872b775..bafc2f088 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -48,7 +48,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -57,27 +56,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=6.verified.txt index 27291483f..f13e5398c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=96_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -48,7 +48,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -57,27 +56,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=0.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=0.verified.txt index bad300b03..83e316c0c 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=0.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=0.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -48,7 +48,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -99,27 +98,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=2.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=2.verified.txt index 03f0e33c8..6073c7c8f 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=2.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=2.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -48,7 +48,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -99,27 +98,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=4.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=4.verified.txt index d235c2ad8..f94daec0b 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=4.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=4.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -49,7 +49,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -100,27 +99,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } diff --git a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=6.verified.txt b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=6.verified.txt index 6fd6a8086..48dfa6702 100644 --- a/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=6.verified.txt +++ b/test/StronglyTypedIds.Tests/Snapshots/SourceGenerationHelperSnapshotTests.GeneratesIdCorrectly_ns=_converter=98_backingType=ObjectId_implementations=6.verified.txt @@ -1,4 +1,4 @@ -//------------------------------------------------------------------------------ +//------------------------------------------------------------------------------ // // This code was generated by the StronglyTypedId source generator // @@ -49,7 +49,6 @@ return value switch { string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)), - string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty, _ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"), }; } @@ -100,27 +99,12 @@ { public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args) { - if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId) - { - return new MyTestId(context.Reader.ReadObjectId()); - } - else - { - context.Reader.SkipValue(); - return MyTestId.Empty; - } + return new MyTestId(context.Reader.ReadObjectId()); } - + public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value) { - if (value.Value == MongoDB.Bson.ObjectId.Empty) - { - context.Writer.WriteNull(); - } - else - { - context.Writer.WriteObjectId(value.Value); - } + context.Writer.WriteObjectId(value.Value); } } } From 6854795f9a92d227c01eeaffa3ca0a83ddf7b028 Mon Sep 17 00:00:00 2001 From: Marcin Rogoz Date: Sat, 21 May 2022 00:18:05 +0200 Subject: [PATCH 12/12] Overrode Mongo2Go binaries for Linux platform. --- .../Fixtures/MongoDbFixture.cs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/test/StronglyTypedIds.IntegrationTests/Fixtures/MongoDbFixture.cs b/test/StronglyTypedIds.IntegrationTests/Fixtures/MongoDbFixture.cs index 58e4f1c29..f87d4d0fd 100644 --- a/test/StronglyTypedIds.IntegrationTests/Fixtures/MongoDbFixture.cs +++ b/test/StronglyTypedIds.IntegrationTests/Fixtures/MongoDbFixture.cs @@ -1,4 +1,5 @@ using System; +using System.Runtime.InteropServices; using Mongo2Go; using MongoDB.Driver; using Xunit; @@ -11,7 +12,7 @@ public class MongoDbFixture : IDisposable public MongoDbFixture() { - _runner = MongoDbRunner.Start(); + _runner = MongoDbRunner.Start(binariesSearchPatternOverride: GetBinariesSearchPattern()); var client = new MongoClient(_runner.ConnectionString); Database = client.GetDatabase("IntegrationTest"); } @@ -22,6 +23,12 @@ public void Dispose() { _runner?.Dispose(); } + + // Overrode default binaries search pattern because of problem on some Linux OS versions + private static string GetBinariesSearchPattern() + { + return RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "tools/mongodb-linux*/bin" : default; + } } [CollectionDefinition(Name)]